--排名函式 row_number() rank(), dense_rank(),ntile()
over()函式 為排名函式生成排序順序、
select row_number() over(order
byorderdate)
asromber,salesorderid,orderdate
from
saleslt.salesorderheader
---建立乙個單一的排序
select row_number() over(partition by
year(orderdate),month(orderdate) order
by orderdate) as
rownumber,
salesorderid,orderdate,shipmethod
from
saleslt.salesorderheader
where salesorderid>=
280order
byorderdate;
---row_number()
select rownumber ,salesordernumber salesorderid,orderdate from(
select row_number() over(order
byorderdate,salesorderid)
as rownumber,*
from
saleslt.salesorderheader
where salesorderid>
293)
as q where
rownumber
between
21and
40order
byrownumber;
--rank() dense_rank() eg:例如 張三跟王五 都是第二名 那麼他們的rank的值應為2
select productid,count(*) as
'count
'from
saleslt.salesorderdetail
group
byproductid
order
bycount(*
);
select productid,salescount,rank() over(order
by salescount) as
'ranks',
dense_rank()
over(order
by salescount) as
'dense_rank
'from (select productid,count(*) as salescount from
saleslt.salesorderdetail
group
by productid) as q groupby'
ranks
';執行結果:對比
通過 對比:rank的結果與 deserank的結果不一樣。。
SQLSERVER常用排名方法
有如下表及資料 一,常用基於什麼排名,按 排序 select row number over order by book price desc as book rank,price,book name from books 如下圖1 二,名次或者分數一樣,並列排名 select top 10 boo...
SQL Server2005中四個排名函式
排名函式是sql server2005新加的功能。在sql server2005中有如下四個排名函式 1.row number 2.rank 3.dense rank 4.ntile 下面分別介紹一下這四個排名函式的功能及用法。在介紹之前假設有乙個t table表,表結構與表中的資料如圖1所示 圖1...
sql server中排名的問題
rank,dense rank,row number區別 一 語法 用法 rank over partition by col1 order by col2 dense rank over partition by col1 order by col2 row number over partiti...