0

I'm currently trying to use a case statement before a group by command. Getting the below error

ORA-00979: not a GROUP BY expression 00979. 00000 - "not a GROUP BY expression" *Cause:
*Action: Error at Line: 188 Column: 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
;
Serg
  • 22,285
  • 5
  • 21
  • 48
user1060187
  • 957
  • 5
  • 12
  • 28

2 Answers2

1

You need to check the aggregate sum(quantity), not quantity

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"
Serg
  • 22,285
  • 5
  • 21
  • 48
0

You must add Quantity to your group by.

group by "SIDE", "TRADEDATE", "ACCOUNTNO", "CUSIP", SUM("QUANTITY")
t.m.adam
  • 15,106
  • 3
  • 32
  • 52