简体   繁体   中英

Select max value from different column in a row in oracle database

My table in oracle database is like this:

Rollno  |   mark1   |   Mark2   |   mark3
--------+-----------+-----------+--------+
101     |   10      |   20      |   30
102     |   22      |   44      |   08
103     |   55      |   11      |   14
--------+-----------+-----------+--------+

And I want output like

Rollno  |   MAX(Mark1,mark2,mark3)
--------+-------------------------+
101     |   30
102     |   44
103     |   55
--------+-------------------------+

Any query suggestion?

Use GREATEST function

SELECT Rollno, GREATEST (mark1, Mark2, mark3) AS Maxv
FROM yourtable

ANSI SQL compliant answer, written before Oracle was specified:

Use CASE to find largest column value:

select Rollno, case when mark1 > mark2 and mark1 > mark3 then mark1
                    when mark2 > mark3 then mark2
                    else mark3 end as max_value
from tablename

( NULL 's not considered here.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM