ORACLE按照兩列中最大值進行排序

2021-09-30 15:29:50 字數 1373 閱讀 9569

假設有下面的一張表:

create table foo(

fooid number(10),

firstcol number(10),

secondcol number(10)

);

為了方便測試,隨機產生一些資料:

declare

n_fooid foo.fooid%type;

n_firstcol foo.firstcol%type;

n_second foo.secondcol%type;

begin

n_fooid :=0;

delete from foo;

for i in 0 .. 200 loop

n_fooid := n_fooid+1;

select trunc(dbms_random.value(0, 200)),

trunc(dbms_random.value(0, 200))

into n_firstcol,n_second

from dual;

insert into foo(fooid,firstcol,secondcol)

values(n_fooid,n_firstcol,n_second);

end loop;

end;

下面是生成的資料:

fooid   firstcol  secondcol

1 1 19

2 134 172

3 25 176

4 99 56

5 41 17

6 65 55

7 73 0

要求: 需要按照firstcol與secondcol中的最大值進行排序。

第乙個解決方案:

select * 

from foo t

order by (case when t.firstcol>t.secondcol then t.firstcol else t.secondcol end);

第二個解決方案:

select *

from foo t

order by greatest(t.firstcol,t.secondcol);

第二個方案明顯要比第一種寫法好很多,如果是在多列的情況下進行比較,因為總不可能寫n多的case when then else end來進行多行的取最大值。看來oracle總是比我們考慮的多一些,因此提供了greatest這個函式。

ORACLE按照兩列中最大值進行排序

oracle按照兩列中最大值進行排序 假設有下面的一張表 sql create table foo www.2cto.com fooid number 10 firstcol number 10 secondcol number 10 為了方便測試,隨機產生一些資料 sql declare www....

求陣列中最大值

所有c語言 都是在loadrunner中執行 action 定義乙個int陣列 int len 記錄陣列元素個數 int max 所求的最大值 int i 迴圈變數 loadrunnerg中,不能在for迴圈中定義變數 len sizeof a sizeof int 陣列元素個數 陣列所佔字元數 陣...

oracle多列最大值的sql

select id,r,sr,kbn from select distinct id row number over partition by id order by r desc rn,row number over partition by id,r order by sr desc rn2,r...