使用Case语句更改单元格的值

时间:2014-03-22 02:12:46

标签: sql case

任何人都可以帮我弄清楚我在做错了什么吗?当我尝试运行它时,我收到以下错误消息,“ORA-00937:不是单组组功能”。

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;

我感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

您需要按所有非聚合列进行分组:

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;

我还注意到你有两个列称为“价格”(案例和回合都共享相同的别名“价格”)。