我的数据库中有两个表销售和设备:
销售
SaleDate EmployeeID AppID Qty
---------- ---------- ----- -----------
2010-01-01 1412 150 1
2010-01-05 3231 110 1
2010-01-03 2920 110 2
2010-01-13 1412 100 1
2010-01-25 1235 150 2
2010-01-22 1235 100 2
2010-01-12 2920 150 3
2010-01-14 3231 100 1
2010-01-15 1235 300 1
2010-01-03 2920 200 2
2010-01-31 2920 310 1
2010-01-05 1412 420 1
2010-01-15 3231 400 2
电器设备
ID AppType StoreID Cost Price
---- -------------------- ------- ------------- -------------
100 Refrigerator 22 150 250
110 Refrigerator 20 175 300
150 Television 27 225 340
200 Microwave Oven 22 120 180
300 Washer 27 200 325
310 Washer 22 280 400
400 Dryer 20 150 220
420 Dryer 22 240 360
如何获取此结果表。 (列出了冰箱和每次销售的销售数量,也显示了总销售价格(总销售价格=数量*价格)。
AppID AppType Qty total sale price
----- ---------------- ----------- ----------------
110 Refrigerator 1 300
110 Refrigerator 2 600
100 Refrigerator 1 250
100 Refrigerator 2 500
100 Refrigerator 1 250
我的尝试:
SELECT AppID, AppType, Qty, (Qty * Price) as 'total sale price'
FROM Sales s, Appliances a
WHERE (AppID) IN (SELECT ID FROM Appliances WHERE AppType = 'Refrigerator')
AND (AppType) IN ('Refrigerator')
目前正在制作
AppID AppType Qty total sale price
----- -------------------- ----------- ----------------
110 Refrigerator 1 250
110 Refrigerator 1 300
110 Refrigerator 2 500
110 Refrigerator 2 600
100 Refrigerator 1 250
100 Refrigerator 1 300
100 Refrigerator 2 500
100 Refrigerator 2 600
100 Refrigerator 1 250
100 Refrigerator 1 300
答案 0 :(得分:0)
在join
sales.AppId = appliances.ID
select s.AppId, a.AppType, s.Qty,
a.Price * s.Qty as total
from appliances a
join sales s on s.AppId = a.ID
where a.AppType = 'Refrigerator'
答案 1 :(得分:0)
您可以使用JOIN
:
SELECT
AppID = p.ID,
p.AppType,
s.Qty,
[Total Sales Price] = s.Qty * p.Price
FROM Sales s
INNER JOIN Appliances p
ON p.ID = s.AppID
WHERE p.AppType = 'Refrigerator'
请注意
JOIN
关键字。