Search code examples
sqlpostgresqlgroup-bywindowaggregate

Aggregation level is off (Postgresql)


I have Order data for 2 customers and their order. And I am trying to calculate what the sum for the price is for every customter for that specific order only for product N

Table:

enter image description here

This is my query:

select Customer_ID, Order_ID, Sales_Date,
sum(Price) over (partition by Customer_ID, Order_ID order by Customer_ID, Order_ID)
from orders
group by 1,2,3, Price
order by;

For some reason I do not understand it gives me several rows per same customer. I am trying to get only one row generated per customer and order for product N

This is my current Output: enter image description here

Desired Outcome:

enter image description here


Solution

  • Why are you using window functions? I think you just want aggregation:

    select Customer_ID, Order_ID, Sales_Date,
           sum(Price)
    from orders
    group by 1,2,3;
    

    If you only want one product, add where product = 'N'.