Client First Name Client Last Name Patient Name
John Wick Raffie
John Wick Coco
Rick Thomas Sussie
Peter Parker Hershey
Peter Parker Malt
Rick Thomas Poky
Lucas Desmond Timmy
结果应该是:
Client ID Client First Name Client Last Name Patient Name Patient ID
1 John Wick Raffie 1
1 John Wick Coco 2
2 Rick Thomas Sussie 3
3 Peter Parker Hershey 4
3 Peter Parker Malt 5
4 Rick Thomas Poky 6
5 Lucas Desmond Timmy 7
我所做的是创建一个存储过程,用row_number()
选择客户端表的内容,然后将患者表与row_number()
一起加入,但我想知道我是否可以在只有一个t-sql脚本?
谢谢。
答案 0 :(得分:0)
我认为 DENSE_RANK()功能可以帮助解决您的问题
Select DENSE_RANK() OVER( order by t.[Client First], t.[Client Last Name]) as [Client ID]
, t.[Client First]
, t.[Client Last Name]
, t.[Patient Name]
, DENSE_RANK() OVER( order by t.[Patient Name]) as [Patient ID]
From [Table] t;