Subject: Calculate de max value of two columns
Posted By: cdias Post Date: 11/26/2003 2:15:17 PM
How can I calculate the max value of two columns with a single query.
Suppose I have a table with the following information:

id  col1  col2
--  ----  ----
1   10    20
2   20    30
3   12    23

The query should return the following data:

id  max_value
--  ----------
1   20
2   30
3   23

Thanks in advance,
Carlos Dias
Reply By: azizmasih Reply Date: 11/26/2003 2:23:50 PM
Hi Carlos,

How about the following ?

--------------------------------------------
SQL> select * from t;

        ID       COL1       COL2
---------- ---------- ----------
         1         10         20
         2         20         30
         3         12         23

SQL> select id, max(col) from (
  2  select id, col1 as col from t
  3  union
  4  select id, col2 as col from t
  5  )
  6  group by id;

        ID   MAX(COL)
---------- ----------
         1         20
         2         30
         3         23

SQL>
--------------------------------------------

Cheers,
Prat



Go to topic 6921

Return to index page 997
Return to index page 996
Return to index page 995
Return to index page 994
Return to index page 993
Return to index page 992
Return to index page 991
Return to index page 990
Return to index page 989
Return to index page 988