I am trying to sum-aggregate conditional products for total weight of an order (I hope that makes sense). I get error:
ERROR: aggregate function calls cannot be nested
LINE 6: , SUM ( CASE WHEN pc.type = 'TEES' THEN (SUM (opd.qt...
This is query excerpt:
SELECT DISTINCT
o.work_order_number dn
, SUM(opd.qty) units
, SUM (CASE WHEN pc.type = 'TEES' THEN (SUM (opd.qty) * .75)
WHEN pc.type = 'JERSEYS' THEN (SUM (opd.qty) * 1.5)
END) AS weight
Try:
SELECT o.work_order_number dn
, SUM(opd.qty) units
, SUM ( opd.qty * CASE pc.type
WHEN 'TEES' THEN 0.75
WHEN 'JERSEYS' THEN 1.5
END ) AS weight
FROM ...
GROUP BY o.work_order_number
Well what you can do is nest your select statement E.g
select sum(weight),sum(etc)
from (
SELECT DISTINCT o.work_order_number dn
, (opd.qty) units
, ( CASE WHEN pc.type = 'TEES' THEN ((opd.qty) * .75)
WHEN pc.type = 'JERSEYS' THEN ((opd.qty) * 1.5) END) AS weight)
).
So first select statement handles your case statement and second select statement sums up your fields.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With