Sample data:
product_type |segment_type |promotion_id |promotion_value
-----------------------------------------------------------
Beer |Regional |1 |20
Beer |National |1 |20
Beer |Regional |2 |20
Beer |National |2 |20
Beer |National |3 |30
Beer |Regional |4 |40
Soda |Regional |5 |50
Soda |National |5 |50
Soda |Regional |6 |50
Soda |National |6 |50
Soda |National |7 |15
Soda |Regional |8 |20
Objective: Get the total promotion value grouped by product_type and segment_type (cube) considering distinct promotions. Note that a single promotion can reach one or both segments (Regional and National).
Desired result:
product_type |segment_type |promotion_value
-------------------------------------------------
Beer | |110
Beer |Regional |80
Beer |National |70
Soda | |135
Soda |Regional |120
Soda |National |115
My current SQL is as follows:
SELECT product_Type,
segment_type,
sum(promotion_value)promotion_value
from sample_data
group by product_type,
cube(segment_type)
The current result is:
product_type |segment_type |promotion_value
-------------------------------------------------
Beer | |150
Beer |Regional |80
Beer |National |70
Soda | |235
Soda |Regional |120
Soda |National |115
SQLFiddle: link
Is there a way to achieve the desired result?