I have a table with the following data for sales and inventory (oh):
category sales oh item_num
Clothes 12 10 1
Clothes 11 10 1
Clothes 10 10 1
Clothes 5 10 1
Clothes 8 10 1
Clothes 4 10 1
Clothes 23 10 2
Clothes 5 10 2
Clothes 20 10 2
Clothes 5 10 2
Clothes 13 10 2
Clothes 9 10 2
Food 6 25 3
Food 8 25 3
Food 7 25 3
Food 14 25 3
I am trying to query this table to get a sum of both the sales and oh columns by category:
SELECT category, SUM(sales) AS sales, SUM(oh) AS oh
FROM data
GROUP BY category
However, the problem is I need the SUM(oh) to only sum distinct items but the SUM(sales) to sum all the values. So the result should be:
category sales oh
Clothes 125 20
Food 35 25
I tried SUM(DISTINCT oh), but that only works for distinct oh values not distinct items. I really need something like SUM(DISTINCT(item_num) oh).
I experimented with various window functions, but could not come up with a solution. Does anyone know how to return this kind of sum on a unique key?
ohvalues are dependent onitem_numand thus have guaranteed fixed values for specificitem_numvalues? If not, how would one choose?