我需要显示平均数量小于或等于300的商家名称。
表S:
BusNAME BusNUM
------- ---------
Bob A1
Lee A2
Ashley A3
表SP:
BusNUM QTY
------- ---------
A1 300
A1 400
A1 100
A2 100
A2 100
A2 200
A3 400
A3 300
这就是我正在尝试的:
SELECT s.BusName
FROM S s
JOIN SP p ON p.BusNum = s.BusNum
GROUP By s.BusName
HAVING AVG(p.QTY) <= 300;
答案 0 :(得分:1)
从表BusNUM
中选择Qty
和SP
的平均值到临时表中,然后进行正常连接!你可以这样做:
SELECT S.BusName
FROM S, (SELECT BusNUM, AVG(QTY) AS Average
FROM SP
GROUP BY BusNUM) Temporal
WHERE S.BusNUM = Temporal.BusNUM
AND Temporal.Average <= 300;