1

I am trying to select the MAX from another query result which returns only one column with values.

The query which gives me the column with all the values from which I want to only select the max is:

SELECT perfy as perf
FROM aasv_chart5
UNION
SELECT perfcumu as perf
FROM aasv_chart5

This returns

PERF
-2.9
1.4
12.5
14

now I want to select the Max value from that. I tried something like..

select MAX(SELECT perfy as perf
    FROM aasv_chart5
    UNION
    SELECT perfcumu as perf
    FROM aasv_chart5) FROM aasv_chart5

2 Answers 2

3

use your first query as a subquery, but use MAX on a field, not on the query

SELECT MAX(perf) from 
  (SELECT perfy as perf
   FROM aasv_chart5
   UNION
   SELECT perfcumu as perf
   FROM aasv_chart5) s

depending on your dbms, your could also do

select max(greatest(perfy, perdcumu))
from aasv_chart5

or if greatest in not there

select max(case when perfy > perdcumu then perfy else perdcumu end)
from aasv_chart5

EDIT

from your comment, you can do

select id, 
  (select max(greatest(perfy, perdcumu))
  from aasv_chart5) as maxVal
from aasv_chart5

or

select id, s.maxval
from aasv_chart5
cross join  (select max(greatest(perfy, perdcumu)) as maxval
  from aasv_chart5) s
Sign up to request clarification or add additional context in comments.

4 Comments

Great! Much appreciated
Now how would I add select of another column (let's say the ID column). So I have 4 rows in my table and want the IDs (1,2,3,4) and also the MAX as second column (the MAX in every row)
@Silvanmidix what's your dbms, by the way ?
It's Oracle. I am querying with iReport and because the software (iReport) is not very smart I need to put the data together as nicely as possible.
1
SELECT max(abc.perf)
FROM
(SELECT perfy as perf
    FROM aasv_chart5
    UNION
    SELECT perfcumu as perf
    FROM aasv_chart5) as abc

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.