我写了以下查询:
SELECT DISTINCT gno, avg(weight) as x
FROM Edge
GROUP BY gno
HAVING avg(weight)<max(weight)/2
并得到这个(正确的)表格:
现在我想使用右列并在那里找到最大值并仅显示该行。 我怎么能这样做?
答案 0 :(得分:1)
如果我理解正确,请使用order by
和limit
:
SELECT gno, avg(weight) as x
FROM Edge
GROUP BY gno
HAVING avg(weight)<max(weight)/2
ORDER BY avg(weight) desc
LIMIT 1;
请注意,使用group by
时,您不需要select distinct
。
答案 1 :(得分:1)
您始终可以使用CTEs撰写此类查询:
WITH averages AS (
SELECT DISTINCT gno, avg(weight) as x
FROM Edge
GROUP BY gno
HAVING avg(weight)<max(weight)/2
)
SELECT MAX(x) FROM averages;