Oracle错误与分组依据

时间:2017-07-18 16:57:58

标签: sql oracle

我目前正在尝试在group by命令之前使用case语句。得到以下错误

  

ORA-00979:不是GROUP BY表达式   00979. 00000 - “不是GROUP BY表达式”   *原因:
  *动作:行错误:188列:12

select "CUSIP", "SIDE", TRADEDATE, ACCOUNTNO, SUM("QUANTITY") as CompressedQuantity,

(case when quantity = 0 THEN 0 ELSE trunc(sum("PRICE"*"QUANTITY")/sum("QUANTITY"),6) END) as AVERAGEPRICE
from rawtable
group by "SIDE", "TRADEDATE", "ACCOUNTNO", "CUSIP")

select CompressedQuantity,"SIDE", "TRADEDATE", "ACCOUNTNO", "CUSIP" FROM compresstable
;

2 个答案:

答案 0 :(得分:1)

您需要检查汇总金额(数量),而不是数量

select "CUSIP", "SIDE", TRADEDATE, ACCOUNTNO, SUM("QUANTITY") as CompressedQuantity,
    case when sum(quantity) = 0 THEN 0 ELSE trunc(sum("PRICE"*"QUANTITY")/sum("QUANTITY"),6) END as AVERAGEPRICE
from rawtable
group by "SIDE", "TRADEDATE", "ACCOUNTNO", "CUSIP"

答案 1 :(得分:0)

您必须将数量添加到您的论坛。

group by "SIDE", "TRADEDATE", "ACCOUNTNO", "CUSIP", SUM("QUANTITY")