How to get max value from different columns of a table ?

Suppose Input table is
COL1 COL2 COL 3

A 1 -3
B -2 6
c 3 -4
D 9 10

In sql query I want the desired output as follows

COL1 COL n

A 1
B 6
C 3
D 10

Please write the query to get this output

Questions by bab.mishra

Showing Answers 1 - 21 of 21 Answers

punitnb26

  • Apr 30th, 2011
 

Try following query. I have used emp table as example
select ename, max(val) from
(
select ename, sal val from emp
union
select ename, comm val from emp
) max_val
group by ename

  Was this answer useful?  Yes

dbxplorer

  • Jul 21st, 2011
 

Hi Raghu,

Query is right but having drawback that it doesn't consider NULL value in comparison, so doesn't show value on comparing with NULL.

  Was this answer useful?  Yes

Girdhar Tripathi

  • Jul 26th, 2011
 



select * from (select max(sal) from emp ) max_sal,(select max(empno) from emp ) max_empno

  Was this answer useful?  Yes

dbxplorer

  • Jul 26th, 2011
 

It's better to write that query in that way:

select max(empno),max(sal) from emp;

Above will give a result but now with performance.

  Was this answer useful?  Yes

Pranit

  • Jul 28th, 2011
 

Code
  1. SELECT max(col1),max(col2),max(col3),max(col4) FROM tblname

  Was this answer useful?  Yes

Thakkar Zankhana

  • Aug 5th, 2011
 

Code
  1. CREATE TABLE tmp_z (col1 VARCHAR2(20), col2 NUMBER, col3 NUMBER);

  2.  

  3. INSERT INTO tmp_z VALUES('A', 1, 3);

  4. INSERT INTO tmp_z VALUES('B', 2, 6);

  5. INSERT INTO tmp_z VALUES('C', 3, 4);

  6. INSERT INTO tmp_z VALUES('D', 9, 10);

  7.  

  8. COMMIT;

  9.  

  10. SELECT Z.COL1,

  11.        DECODE(MOD(ROWNUM, 2),

  12.               0,

  13.               GREATEST(Z.COL2, Z.COL3),

  14.               LEAST(Z.COL2, Z.COL3))

  15.   FROM TMP_Z Z;


  Was this answer useful?  Yes

milind88

  • Aug 10th, 2011
 

NAME COL1 COL2
---------- ---------- ----------
A 1 -3
B -2 6
C 3 -4
D 9 10

SQL> select name,
2 case
3 when col1>col2 then
4 col1
5 else
6 col2
7 end Highest
8 from aaa;

NAME HIGHEST
---------- ----------
A 1
B 6
C 3
D 10

  Was this answer useful?  Yes

GoroAU

  • Aug 17th, 2011
 

Hi,
GREATEST with NULLs

Code
  1. WITH t AS

  2. (

  3. SELECT 'A' AS COL1, 1 AS COL2, -3 AS COL3 FROM dual union ALL

  4. SELECT 'B' ,-2   ,6  FROM dual union ALL

  5. SELECT 'C' ,3    ,-4 FROM dual union ALL

  6. SELECT 'D' ,9    ,10 FROM dual union ALL

  7. SELECT 'E' ,NULL ,7  FROM dual

  8. )

  9. SELECT COL1, greatest(nvl(COL2,COL3),nvl(COL3,COL2)) COLn FROM t

  Was this answer useful?  Yes

Hello,

For my query example I have used the table test_tbl, and the columns col, col1, col2.

It also does consider NULL value in comparison, so it will show value on comparing with NULL, and for every
row [Col, Col1, Col2 ] it displays as result
Col, MAX(Col1,Col2) mxm

Have a good day

Code
  1. SELECT a.col, (SELECT max(b.val) FROM (SELECT col, col1 val FROM test_tbl

  2. union SELECT col, col2 val FROM test_tbl) b WHERE a.col = b.col) mxm FROM test_tbl a;

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions