我正在尝试返回所有舱位名称,这些舱位名称的容纳人数大于其舱位类型可以容纳的平均人数。
SELECT CNAME AS "CABIN NAME", NUMACCOM AS "Can Accomodate"
FROM cabinWH
WHERE NUMACCOM > (SELECT AVG(NUMACCOM) FROM cabinWH GROUP BY CTYPE)
错误:
子查询返回了多个值。当子查询遵循=,!=,<,<=,>,> =或将子查询用作表达式时,不允许这样做。
答案 0 :(得分:1)
我认为您想考虑一种特定的类型,而不是按类型分组,这当然会给您多行,每行一种:
SELECT CNAME AS "CABIN NAME", NUMACCOM AS "Can Accomodate"
FROM cabinWH C1
WHERE NUMACCOM > (SELECT AVG(C2.NUMACCOM) FROM cabinWH C2 where C2.CTYPE = C1.CTYPE)
注意:如果要对整个结果集取平均值,则不需要分组。
答案 1 :(得分:0)
您应该在子查询中放置where子句,以确保从中得到单个结果:
SELECT outerCabin.CNAME AS "CABIN NAME", outerCabin.NUMACCOM AS "Can Accomodate"
FROM cabinWH outerCabin
WHERE NUMACCOM >
(SELECT AVG(innerCabin.NUMACCOM) FROM cabinWH innerCabin
where outerCabin.CTYPE = innerCabin.CTYPE GROUP BY innerCabin.CTYPE)
尝试一下。