我在SQL Server中有下表;
DesiredRowNo. Customer Date NID SID Contacts AG
1 AAA 04/09/2018 15357989260016600 100743 1 1
1 AAA 04/09/2018 15358110980018300 100743 1 1
1 AAA 04/09/2018 15358197620012800 100743 1 1
2 AAA 21/09/2018 15358902310016400 100743 1 1
2 AAA 21/09/2018 15358999300015200 100743 1 1
3 AAA 29/09/2018 15359624190012200 100743 1 1
3 AAA 29/09/2018 15359896100010500 100702 1 1
4 AAA 30/09/2018 15360551640011900 100743 1 1
1 BBB 05/09/2018 15357989260016600 100743 1 1
1 BBB 05/09/2018 15358110980018300 100743 1 1
2 BBB 08/09/2018 15358197620012800 100743 1 1
3 BBB 15/09/2018 15358902310016400 100743 1 1
3 BBB 15/09/2018 15358999300015200 100743 1 1
4 BBB 19/09/2018 15359624190012200 100743 1 1
5 BBB 24/09/2018 15359896100010500 100702 1 1
5 BBB 24/09/2018 15360551640011900 100743 1 1
我需要第一列(DesiredRowNo.(根据客户和日期显示排名/行号。因此,对于客户 AAA,具有相同日期的所有行都排名为 1,然后下一个唯一日期排名为 2,依此类推。(必须根据日期按顺序排列(
请参阅上面的DesiredRowNo. 值,以更好地解释我想要实现的目标。
我尝试使用;
ROW_NUMER() over (partition by Customer, date order by date)
但这似乎并没有按要求给出正确的顺序。
有什么想法吗?
试试这个:
select tab.DesiresRowNo.,final.Customer,
final.NID,final.SID,final.Contacts,final.AG
from Table final
left join
( select
ROW_NUMBER() OVER(ORDER BY Customer ASC) AS DesiresRowNo.,
Customer,Date
from Table
group by Customer,Date )tab
on final.Customer=tab.Customer
and final.Date=tab.Date
哎呀,这是一个巨大的疏忽。我用这个修复了它:
DENSE_RANK() over (partition by Customer order by date)
感谢您的掌舵拉努