SQL 2005的行号(转)

select CAST(ROW_NUMBER() OVER(order by Name) AS varchar(32)) AS rowindex可以将行号取出来转换成varchar型的显示在页面上

1
ROW_NUMBER
SELECT ProductID, Name, Price, 
ROW_NUMBER() OVER(ORDER BY Price DESC) As PriceRank
FROM Products
ProductID Name Price PriceRank
8Desk495.00001
10Executive Chair295.00002
9Chair125.00003
5Mouse14.95004
6Mousepad9.99005
11Scissors8.50006
4Stapler7.95007
3Binder1.95008
...
SELECT c.Name, o.DateOrdered, tab.TotalOrderAmount, 
ROW_NUMBER() OVER (PARTITION BY c.CustomerID ORDER BY TotalOrderAmount DESC) AS BestCustomer
FROM vwTotalAmountBilledPerOrder AS tab
INNER JOIN Orders AS o ON
o.OrderID = tab.OrderID
INNER JOIN Customers AS c ON
c.CustomerID = o.CustomerID
Name DateOrdered TotalOrderAmount BestCustomer
Bob12/1/200512649.99001
Bob12/19/2005265.85002
Tito12/22/200514.95001
Tito12/18/200512.44002
Darren1/2/2006620.00001
Bruce1/5/200614.95001
Bruce1/4/20069.99002
Lee Ann1/3/20068.50001
...

2 RANK

SELECT c.Name, o.DateOrdered, tab.TotalOrderAmount, 
RANK() OVER (ORDER BY TotalOrderAmount DESC) AS BestCustomer
FROM vwTotalAmountBilledPerOrder AS tab
INNER JOIN Orders AS o ON
o.OrderID = tab.OrderID
INNER JOIN Customers AS c ON
c.CustomerID = o.CustomerID
Name DateOrdered TotalOrderAmount BestCustomer
Bob12/1/200512649.99001
Darren1/2/2006620.00002
Bob12/19/2005265.85003
Tito12/22/200514.95004
Bruce1/5/200614.95004
Tito12/18/200512.44006
Bruce1/4/20069.99007
Lee Ann1/3/20068.50008
...

3 NTILE

SELECT ProductID, Name, Price, NTILE(4) OVER (ORDER BY Price DESC) as Quartile
FROM Produts

ProductID Name Price Quartile
8Desk495.00001
10Executive Chair295.00001
9Chair125.00002
5Mouse14.95002
6Mousepad9.99003
11Scissors8.50003
4Stapler7.95004
3Binder1.95004
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值