1

我有一个如下所示的表:

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

我想对 INDIVIDUAL ROWS 进行排序并根据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 进行排序,依此类推......

什么是韭菜的有效方法?

问候,

4

1 回答 1

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.
于 2012-11-30T19:35:53.217 回答