我有一张如下表所示的表格:
Name LastName tPoints aPoints sPoints gPoints type
John Johnny 15 14 13 10 1
Joe P. 12 11 26 10 1
Matt Q. 11 26 37 44 2
Sorine P. 55 9 8 7 2
Ali Ahmed 30 44 88 65 2
... ... .. .. .. .. 3
3
我想根据TYPE
注意:i can't use order by in oracle because it sorts only 1 row and the others
is sorted based on the first row
我不想将表拆分为单独的表,然后对其进行排序,然后将其更新回原始表。
所以,对于tPoints
,输出看起来像这样 - 我需要显示所有
15 - John Johnny
12 - Joe P.
和aPoints
44 - Ali Ahmed
26 - Matt Q.
9 - Sorine P.
依旧......
简而言之,如果type = 1则按降序排序tPoints,如果type = 2则排序aPoints,如果type = 3则排序sPoints,依此类推......
什么是一种有效的方法来韭菜?
问候,
答案 0 :(得分:1)
为简单起见,此示例仅包含两种类型。根据需要添加任意数量的类型。
SQL> with t1(Name1, LastName, tPoints, aPoints, sPoints, gPoints, type1) as(
2 select 'John' , 'Johnny', 15, 14, 13, 10, 1 from dual union all
3 select 'Joe' , 'P.' , 12, 11, 26, 10, 1 from dual union all
4 select 'Matt' , 'Q.' , 11, 26, 37, 44, 2 from dual union all
5 select 'Sorine', 'P.' , 55, 9 , 8 , 7, 2 from dual union all
6 select 'Ali' , 'Ahmed' , 30, 44, 88, 65, 2 from dual
7 )
8 select type1
9 , tpoints
10 , apoints
11 , name1
12 , Lastname
13 from t1
14 order by case when type1=1 then tpoints else type1 end desc,
15 case when type1=2 then apoints else type1 end desc;
TYPE1 TPOINTS APOINTS NAME1 LASTNAME
---------- ---------- ---------- ------ --------
1 15 14 John Johnny
1 12 11 Joe P.
2 30 44 Ali Ahmed
2 11 26 Matt Q.
2 55 9 Sorine P.