1

I have a table that consists of an order, items on that order, and then the quantity of the item ordered.

What I would like to do is create an additional column for 'Order quantity' which is the sum of item quantities grouped by order (see graphic of table below... order B has 30 total quantity split across three lines)

I can easily accomplish this using sum and partition:

SUM(quantity) OVER (PARTITION BY order_id) order_qty

However, what I need to is then filter to only those orders having quantity > 20. When I try to add that criteria to the WHERE or HAVING clauses, I get this error:

ORA-30483: window  functions are not allowed here

One solution appears to be to wrap the whole SQL block inside of another SELECT FROM statement, and then add a WHERE clause to filter by order_qty. Overall that seems sloppy and non-intuitive... Is there a better solution to filter based on an aggregated value that is partitioned at a higher level?

enter image description here

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
corycorycory
  • 1,448
  • 2
  • 23
  • 42

1 Answers1

1

Replace with

SUM(quantity) OVER (PARTITION BY order_id order by 1) order_qty
ibrahim
  • 63
  • 6