0

The given below is the query:

SELECT  
   states.name, 
   equipment.type, 
   sum(equipment_events.total), 
   (SUM(equipment_locations.amount) - SUM(equipment_events.total)) 
FROM 
   States, 
   Locations,
   Equipment_locations, 
   equipment, 
   equipment_events, 
   events 
WHERE 
   equipment.id = equipment_events.equipment_id 
   and states.id = locations.state_id 
   and locations.id = events.location_id 
   and equipment.id = equipment_events.equipment_id 
GROUP BY  
   states.name;
Luuk
  • 12,245
  • 5
  • 22
  • 33
Aryan
  • 1
  • 5
    You seem to possibly have a cross join in there, which could explain the bad performance. Tip of the day: Stop writing your table joins using the old school join syntax. Instead, always use modern, explicit joins. – Tim Biegeleisen Sep 25 '20 at 03:42
  • @TimBiegeleisen haha – Matt G Sep 25 '20 at 03:54
  • 1
    Why are you choosing not to use proper, explicit, **standard**, readable `JOIN` syntax? If you are learning SQL, it is all the more important that you learn the correct syntax for such queries. – Gordon Linoff Sep 25 '20 at 12:35
  • Have a look at https://www.w3schools.com/sql/sql_join.asp for modern join syntax. – Bruce Sep 25 '20 at 15:32
  • Is this in MSSQL? then please add query execution plan, see: [how-do-i-obtain-a-query-execution-plan-in-sql-server](https://stackoverflow.com/questions/7359702/how-do-i-obtain-a-query-execution-plan-in-sql-server) – Luuk Sep 25 '20 at 15:34
  • Thank you everyone, I will try doing it using Joins, yes, I am learning SQL at the moment. – Aryan Sep 27 '20 at 02:46

0 Answers0