2012-11-30 135 views
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 

我想显示根据各行进行排序,并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,则排序s点,等等......

什么将是一个有效的方法来韭菜?

Regards,

+0

行或列?你能给我们一个具体的例子吗? – Will

+0

只需将其导出为ex​​cel,然后重新排列它即可。 –

+0

@nathanhayfield这是一个存储过程,不可能,但是谢谢 – user1683987

回答

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. 
+0

这可能工作。只是想知道,如果我想使用上面的方法,我会遇到问题,但是将'insert into select'插入到新表中,所以我可以显示,但是我想要 - ? – user1683987

+1

@ user1683987不,你不会遇到'insert into table select'的问题。而且,插入一个有序的数据集也没有任何意义。 –

+0

我需要显示每个类型单独排序,所以我想这是我唯一的选择。除非你知道另一种显示方式 - – user1683987

相关问题