gpt4 book ai didi

sql - 如何与 "row_number() over (partition by [Col] order by [Col])"相反

转载 作者:行者123 更新时间:2023-12-02 11:26:26 25 4
gpt4 key购买 nike

我正在尝试合并数据表中的重复条目并给它们一个新的数字。

这是一个示例数据集 ( runnable copy )

declare @tmpTable table
(ID Varchar(1),
First varchar(4),
Last varchar(5),
Phone varchar(13),
NonKeyField varchar(4))

insert into @tmpTable select 'A', 'John', 'Smith', '(555)555-1234', 'ASDF'
insert into @tmpTable select 'B', 'John', 'Smith', '(555)555-1234', 'GHJK'
insert into @tmpTable select 'C', 'Jane', 'Smith', '(555)555-1234', 'QWER'
insert into @tmpTable select 'D', 'John', 'Smith', '(555)555-1234', 'RTYU'
insert into @tmpTable select 'E', 'Bill', 'Blake', '(555)555-0000', 'BVNM'
insert into @tmpTable select 'F', 'Bill', 'Blake', '(555)555-0000', '%^&*'
insert into @tmpTable select 'G', 'John', 'Smith', '(555)555-1234', '!#RF'

select row_number() over (partition by First, Last, Phone order by ID) NewIDNum, *
from @tmpTable order by ID

现在它给了我结果

NewIDNum             ID   First Last  Phone         NonKeyField
-------------------- ---- ----- ----- ------------- -----------
1 A John Smith (555)555-1234 ASDF
2 B John Smith (555)555-1234 GHJK
1 C Jane Smith (555)555-1234 QWER
3 D John Smith (555)555-1234 RTYU
1 E Bill Blake (555)555-0000 BVNM
2 F Bill Blake (555)555-0000 %^&*
4 G John Smith (555)555-1234 !#RF

但这与我想要的相反,NewIDNum 每当发现新的按键组合时都会重置其计数器。我希望所有相同的组合都具有相同的 ID。因此,如果它按照我想要的方式运行,我会得到以下结果

NewIDNum             ID   First Last  Phone         NonKeyField
-------------------- ---- ----- ----- ------------- -----------
1 A John Smith (555)555-1234 ASDF
1 B John Smith (555)555-1234 GHJK
2 C Jane Smith (555)555-1234 QWER
1 D John Smith (555)555-1234 RTYU
3 E Bill Blake (555)555-0000 BVNM
3 F Bill Blake (555)555-0000 %^&*
1 G John Smith (555)555-1234 !#RF

获得我想要的结果的正确方法是什么?

<小时/>

我没有在原始帖子中包含此要求:如果有更多行,我需要 NewIDNum 在后续运行此查询时为现有行生成相同的数字被添加(假设如果在 ID 列上完成排序,则所有新行将具有更高的 ID“值”)

因此,如果稍后完成以下操作

insert into @tmpTable select 'H', 'John', 'Smith', '(555)555-1234', '4321'
insert into @tmpTable select 'I', 'Jake', 'Jons', '(555)555-1234', '1234'
insert into @tmpTable select 'J', 'John', 'Smith', '(555)555-1234', '2345'

再次运行正确的查询将给出

NewIDNum             ID   First Last  Phone         NonKeyField
-------------------- ---- ----- ----- ------------- -----------
1 A John Smith (555)555-1234 ASDF
1 B John Smith (555)555-1234 GHJK
2 C Jane Smith (555)555-1234 QWER
1 D John Smith (555)555-1234 RTYU
3 E Bill Blake (555)555-0000 BVNM
3 F Bill Blake (555)555-0000 %^&*
1 G John Smith (555)555-1234 !#RF
1 H John Smith (555)555-1234 4321
4 I Jake Jons (555)555-1234 1234
1 J John Smith (555)555-1234 2345

最佳答案

您可以使用dense_rank():

dense_rank() over (order by First, Last, Phone) as NewIDNum

为了回复您的评论,您可以使用相同的(名字、姓氏、电话号码) 组合对每组行的旧 Id 列的最小值进行排序:

select  *
from (
select dense_rank() over (order by min_id) as new_id
, *
from (
select min(id) over (
partition by First, Last, Phone) as min_id
, *
from @tmpTable
) as sub1
) as sub3
order by
new_id

关于sql - 如何与 "row_number() over (partition by [Col] order by [Col])"相反,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12695858/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com