Reputation: 2516
I wrote the following query:
SELECT DISTINCT gno, avg(weight) as x
FROM Edge
GROUP BY gno
HAVING avg(weight)<max(weight)/2
and got this (correct) table:
now i want to use the right column and find the max value there and show only that line. how can I do it?
Upvotes: 0
Views: 362
Reputation: 13306
You can always compose queries like this using 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;
(but be aware that CTEs are optimization fences)
Upvotes: 1
Reputation: 1269703
If I understand correctly, use order by
and 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;
Note that when using group by
, you don't need select distinct
.
Upvotes: 1