0
我有2個表,需要加以比較:比較兩個表其中一個分組
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
不過,目前我正在清點數量的倍數給我一個不正確的差異。如何比較'訂單'中的數量(按產品分組)和「庫存」(未分組)中的數量?
感謝, 德里克
product5/Gray的order_quantity是不是470,總結意大利和英國的訂單? – 2011-12-20 22:59:55