0

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: enter image description here

now i want to use the right column and find the max value there and show only that line. how can I do it?

2 Answers 2

1

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)

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.