I have 2 tables which need to be compared:
TABLE 'orders'
customer|product |colour|order_quantity
Germany |product1|black |100
Germany |product1|red |200
Germany |product3|yellow|150
UK |product1|black |450
UK |product2|green |350
UK |product5|grey |100
Italy |product1|grey |200
Italy |product2|black |300
Italy |product3|yellow|400
Italy |product3|black |800
Italy |product4|blue |250
Italy |product5|grey |370
TABLE 'inventory'
product |colour|inventory_quantity
product1|black |830
product1|red |250
product1|grey |600
product1|mango |120
product2|green |50
product2|black |350
product3|yellow|900
product3|black |900
product4|blue |210
product4|black |50
product4|green |25
product5|grey |700
Output wanted:
product |colour|order_quantity|inventory_quantity|difference
product1|black |550 |830 |180
product1|red |200 |250 |50
product1|grey |200 |600 |400
product1|mango |0 |120 |120
product2|green |350 |50 |-300
product2|black |300 |350 |50
product3|yellow|550 |900 |350
product3|black |800 |900 |100
product4|blue |250 |210 |-40
product4|black |0 |50 |50
product4|green |0 |25 |25
product5|grey |370 |700 |330
However, currently I am getting a multiple of inventory quantity giving me an incorrect difference. How do compare the quantity in ‘orders’ (grouped by product), with that available in ‘inventory’ (not grouped)?
Thanks,
Derek
Here’s one option
2nd option to follow:
option 3
Used full outer join because Order or inventory may be null and suspect you still want the difference.