02 December 2015

Oracle SQL : How to get greatest value from multiple columns in a single row

Oracle has inbuilt function GREATEST

Example :

SQL> insert into test values(1,10,20,30);
1 row created.
SQL> insert into test values(2,30,10,20);
1 row created.
SQL> insert into test values(3,50,90,60);
1 row created.
SQL> commit;

Commit complete.

SQL> select * from test;

ID A B C
---------- ---------- ---------- ----------
1 10 20 30
2 30 10 20
3 50 90 60

SQL> select greatest(A,B,C) Max_Val from test;

MAX_VAL
----------
30
30
90

No comments: