我需要计算订单的总价。在这种情况下,公司只销售独特的商品,但它们可能具有相同的广泛产品描述。
这些是相关表格
珠宝
JewelleryID Description Price Year TypeID
----------- -------------------------------------------------- ---------------------- ----------- -----------
1 Gold Necklace 250 1925 1
2 Gold Ring 340 1889 3
3 Gold Broach 570 1901 3
4 Diamond Ring 2300 1952 3
5 Pearl Earrings 460 1936 3
6 Pearl Necklace 780 1939 3
7 Diamond Ring 1250 1898 3
11 Gold Watch 560 1905 2
12 Silver Necklace 275 1876 3
13 Gold Broach 250 1949 3
14 Diamond Earrings 2500 1924 3
15 Gold Earrings 280 1939 1
16 Silver Bracelet 245 1946 3
17 Ruby Ring 550 1915 3
18 Silver Watch 780 1936 4
JewellerySales
SaleID JewelleryID
----------- -----------
1 1
1 7
2 11
3 2
3 12
3 13
4 3
5 13
6 4
6 14
7 5
7 17
8 6
9 15
9 18
销售
SaleID CustomerID SaleDate
----------- ----------- ----------
1 1 2016-06-02
2 2 2016-06-02
3 3 2016-06-03
4 1 2016-06-04
5 4 2016-06-04
6 2 2016-06-06
7 5 2016-06-07
8 6 2016-06-07
9 7 2016-06-08
只是寻找任何指针,因为我在这里完全迷失了。
例如:销售ID 1包含两个项目(JewelleryID 1和7)。总价格是(250 + 1250)1500但是我无法弄清楚如何为每个SaleID显示这个。
答案 0 :(得分:0)
汇总价格以便他们可以加入,然后按销售ID加入表格和分组:
SELECT JewellerySales.SaleID, SUM(Jewellery.Price)
FROM JewellerySales
INNER JOIN Jewellery
ON JewellerySales.JewelleryID=Jewellery.JewelleryID
GROUP BY JewellerySales.SaleID;