0

I used the SUMIFS formula in my google sheet to sum the product cost based on Order Id and Composite Parent SKU if it is equal to SKU. However, I cannot get the result I want using SUM OVER PARTITION BY in SQL.

Here is my data:

Order_Id SKU Composite_Parent_SKU Quantity Unit_Cost Product_Cost
00001 SKU001 1 20 5
00001 SKU001X2 1 30
00001 SKU001 SKU001X2 2 10

What I get if using SUM OVER PARTITION BY:


CASE WHEN Unit_Cost = 0 THEN 0
WHEN Unit_Cost > 0 AND Product_Cost > 0 THEN SUM(Product_Cost * Quantity)
ELSE SUM(Product_Cost * Quantity) OVER (PARTITION BY Order_Id, SKU) END
Order_Id SKU Composite_Parent_SKU Quantity Unit_Cost Product_Cost My_Result
00001 SKU001 1 20 5 15
00001 SKU001X2 1 30
00001 SKU001 SKU001X2 2 10 0

Here is the result that I want:

Order_Id SKU Composite_Parent_SKU Quantity Unit_Cost Product_Cost My_Result
00001 SKU001 1 20 5 5
00001 SKU001X2 1 30 10
00001 SKU001 SKU001X2 2 10

Could anyone please help with this?

Thanks in advance.

Irene
  • 1
  • 1

0 Answers0