Question

Can anyone help me figure out what I am doing wrong here? When I try to run it, I get the following error message, "ORA-00937: not a single-group group function".

SELECT o.order_number, o.order_date, ol.line_number, ol.line_type, ol.sku, 
  CASE WHEN ol.line_type = 'shipping' THEN 'Shipping Charges' 
       WHEN ol.line_type = 'tax' THEN 'Tax Charges' 
       ELSE p.title 
       END AS price,
ROUND(ol.price,2) AS Price, ol.quantity, SUM(ol.price * ol.quantity) AS total_price
FROM hr.bc_orders o
INNER JOIN hr.bc_orderlines ol ON o.order_number = ol.order_number
LEFT JOIN hr.bc_products p ON ol.sku = p.sku
WHERE o.order_number = 'o21010469' 
ORDER BY ol.line_number;

I appreciate any help.

Was it helpful?

Solution

You need to group by all the non-aggregating columns:

SELECT o.order_number, o.order_date, ol.line_number, ol.line_type, ol.sku, 
  CASE WHEN ol.line_type = 'shipping' THEN 'Shipping Charges' 
       WHEN ol.line_type = 'tax' THEN 'Tax Charges' 
       ELSE p.title 
       END AS price,
ROUND(ol.price,2) AS Price, ol.quantity, SUM(ol.price * ol.quantity) AS total_price
FROM hr.bc_orders o
INNER JOIN hr.bc_orderlines ol ON o.order_number = ol.order_number
LEFT JOIN hr.bc_products p ON ol.sku = p.sku
WHERE o.order_number = 'o21010469'
GROUP BY o.order_number, o.order_date, ol.line_number, ol.line_type, ol.sku, 
  CASE WHEN ol.line_type = 'shipping' THEN 'Shipping Charges' 
       WHEN ol.line_type = 'tax' THEN 'Tax Charges' 
       ELSE p.title 
       END,
ROUND(ol.price,2), ol.quantity 
ORDER BY ol.line_number;

I also note you have two columns called "price" (the case and the round both share the same alias "price").

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top