Oracle:从同一行的不同列中选择最大值

时间:2022-06-28 13:21:16

The whole question is pretty much in the title. For each row of the table I'd like to select the maximum of a subset of columns.

整个问题几乎都在标题中。对于表格的每一行,我想选择列子集的最大值。

For example, from this table

例如,从此表中

name m1 m2 m3 m4
A    1  2  3  4
B    6  3  4  5
C    1  5  2  1

the result would be

结果将是

name max
A    4
B    6
C    5

The query must be compatible oracle 8i.

查询必须兼容oracle 8i。

2 个解决方案

#1


57  

Given this test data ...

鉴于此测试数据......

SQL> select *
  2  from your_table
  3  /

NAME         M1         M2         M3         M4
---- ---------- ---------- ---------- ----------
A             1          2          3          4
B             6          3          4          5
C             1          5          2          1

SQL>

... a straightforward GREATEST() call will give the desired result:

...一个简单的GREATEST()调用将给出所需的结果:

SQL> select name
  2          , greatest(m1,m2,m3,m4)
  3  from your_table
  4  /

NAME GREATEST(M1,M2,M3,M4)
---- ---------------------
A                        4
B                        6
C                        5

SQL>

#2


12  

Use GREATEST but also handle possible NULL's

使用GREATEST但也可以处理可能的NULL

SELECT name, GREATEST(NVL(m1,0), NVL(m2,0), NVL(m3,0), NVL(m4,0)) AS "Max"
FROM yourtable

Input:

输入:

name m1 m2 m3 m4
A    1  2  3  4
B    6  3  4  5
C    1  5  2  1

Output:

输出:

NAME Max
A    4
B    6
C    5

SQL Fiddle: http://sqlfiddle.com/#!4/ae268/7/0

SQL小提琴:http://sqlfiddle.com/#!4 / ae268 / 7/0

Input:

输入:

name m1 m2   m3 m4
A    1  2    3  null
B    6  null 4  5
C    1  5    2  1

Output:

输出:

NAME Max
A    3
B    6
C    5

SQL Fiddle: http://sqlfiddle.com/#!4/b1c46/1/0

SQL小提琴:http://sqlfiddle.com/#!4/b1c46/1/0

#1


57  

Given this test data ...

鉴于此测试数据......

SQL> select *
  2  from your_table
  3  /

NAME         M1         M2         M3         M4
---- ---------- ---------- ---------- ----------
A             1          2          3          4
B             6          3          4          5
C             1          5          2          1

SQL>

... a straightforward GREATEST() call will give the desired result:

...一个简单的GREATEST()调用将给出所需的结果:

SQL> select name
  2          , greatest(m1,m2,m3,m4)
  3  from your_table
  4  /

NAME GREATEST(M1,M2,M3,M4)
---- ---------------------
A                        4
B                        6
C                        5

SQL>

#2


12  

Use GREATEST but also handle possible NULL's

使用GREATEST但也可以处理可能的NULL

SELECT name, GREATEST(NVL(m1,0), NVL(m2,0), NVL(m3,0), NVL(m4,0)) AS "Max"
FROM yourtable

Input:

输入:

name m1 m2 m3 m4
A    1  2  3  4
B    6  3  4  5
C    1  5  2  1

Output:

输出:

NAME Max
A    4
B    6
C    5

SQL Fiddle: http://sqlfiddle.com/#!4/ae268/7/0

SQL小提琴:http://sqlfiddle.com/#!4 / ae268 / 7/0

Input:

输入:

name m1 m2   m3 m4
A    1  2    3  null
B    6  null 4  5
C    1  5    2  1

Output:

输出:

NAME Max
A    3
B    6
C    5

SQL Fiddle: http://sqlfiddle.com/#!4/b1c46/1/0

SQL小提琴:http://sqlfiddle.com/#!4/b1c46/1/0