MsSql Group或Order 2不同的列值

时间:2016-06-17 07:37:53

标签: sql sql-server sql-server-2008 group-by sql-order-by

我有一张这样的桌子;

   Product   Price  Type
-----------------------------------------
   TV          10    T1  
   Computer    15    T1
   Banana      2     T2
   Apple       3     T2

我想对这两种不同的类型进行排序。 (MS-SQL)

   Product   Price  Type
-----------------------------------------
   TV          10    **T1**  
   Banana      2     **T2**       
   Computer    15    **T1**
   Apple       3     **T2**

有没有办法做到这一点?

1 个答案:

答案 0 :(得分:1)

您可以在ROW_NUMBER子句中使用ORDER BY

SELECT *
FROM #tbl
ORDER BY
    ROW_NUMBER() OVER(PARTITION BY Type ORDER BY Price),
    Type

ONLINE DEMO