行号相关

时间:2013-10-16 10:10:29

标签: sql

我有这种类型的表。

no    CustID             Amount
1     1234               1000
2     2345                500
3     1234               1000
4     6789                500
5     1234               1000
6     6789                350
7     2345                600    
8     1234                250

我需要这样的输出

No    CustId             Amount
1     1234               1000
2     1234               1000
3     1234               1000
4     1234                250
1     2345                500
2     2345                600
1     6789                500
2     6789                350

请为此提供Sql

3 个答案:

答案 0 :(得分:2)

SELECT row_number() over (partition by CustId order by Amount desc) No, 
CustId, Amount
FROM <table>
ORDER BY CustId, Amount DESC

答案 1 :(得分:1)

由于查询将在T-SQL中运行,因此根据客户ID分组行编号将在T-SQL中运行:

select ROW_NUMBER () over (partition by CustID order by Amount desc) as [No],CustID,Amount
from table1

答案 2 :(得分:-1)

select * from TABLE_NAME order by CustId;