简体   繁体   中英

SQL query: calculating a matrix of totals

My query works, and looks like this:

   SELECT 
     SUM(`WTE`) / 1000 AS WTE
   FROM  `orthoptists` AS o
   LEFT JOIN `instances` AS i
   ON o.instance_FK = i.id
   WHERE i.region = 14 AND o.band = "D" AND o.age = "A"  ;

I have 10 possible values each for o.band and o.age, and I need the result of this query for each permutation of those two values.

I am very new to SQL: is there a way to use a sub-query to produce all the results for one value in o.band (say)? Or even to do the whole lot in one go?

Performance isn't important in this: convenience is.

Try this

   SELECT 
     SUM(`WTE`) / 1000 AS WTE, o.band, o.age
   FROM  `orthoptists` AS o
   LEFT JOIN `instances` AS i
   ON o.instance_FK = i.id
   WHERE i.region = 14
   group by o.band, o.age

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM