sql-server - How to add sequence row numbers based on column values SQL Server
问题描述
I have the following table in 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
I need the 1st column (DesiredRowNo.) to display a rank/row number based on the customer and the date. So for Customer AAA, all the rows with same date ranked as 1, then the next unique date ranked as 2, and so on. (has to be sequential based on date)
See the DesiredRowNo. values above for a better explanation as to what I'm trying to achieve.
Ive tried using;
ROW_NUMER() over (partition by Customer, date order by date)
But this doesnt seem to give the correct sequencing as required.
Any ideas?
解决方案
尝试这个 :
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
推荐阅读
- python - 引用日期时如何更新 dataframe.ix 代码
- sql-update - 如何在 symfony 5 中不使用表单创建更新功能
- discord.py - 我可以在 discord.py 中为 discord.Embed.set_thumbnail 设置 cookie 吗?
- amazon-web-services - 如何将文件从 s3 解压到 lambda /tmp/ 文件夹 - nodeJs
- c# - 带有附加文本的打印列表
- angular - Angular 材质树格式问题
- c# - .Net 中是否有 SChannel 的替代 TLS 实现
- visual-studio - Visual Studio 2019:发布配置文件中的“删除现有文件”选项消失了
- php - 在 WooCommerce 快速订单预览窗口的新列中显示产品缩略图
- java - FFMPEG在将tiff图片转换为png时反转颜色