I have the following two tables:
Table X
Product Type Sales
1 a 1
1 b 1
1 c 0
1 a 2
Table Y
Product Inventory
1 10
I would like my query to return:
Product type sales inventory
1 a 3 10
1 b 1 0
1 c 0 0
Issue is using aggregate functions and not over counting the inventory. Example:
select X.product, X.type, sum(X.sales), sum( case when X.sales > 0 then Y.inventory else 0 end)
from x,y
where x.product = y.product
group by x.product,x.type
If you are basing the final result on thesales, this should give you the result:See SQL Fiddle with Demo
I am unsure why you have the
inventoryforblisted as zero in your sample result considering it has a total sales greater than zero.Edit #1:
Based on your comments, I would suggest looking at using
row_number():See SQL Fiddle with Demo
Or if you cannot group by
inventory, then join outside of the subquery:See SQL Fiddle with Demo