在ROW_NUMBER()中包含行,其中大于1的行包括第一个值

问题描述 投票:1回答:1

我需要找到具有相同名称,姓氏和出生日期的用户。我必须包括每个人的客户ID,所以我不能将COUNT(*)与group by一起使用。我在CTE中使用由这三列分隔的ROW_NUMBER()。但我需要包括所有多个值,包括where rownumber = 1

这就是我现在拥有的:

;WITH 
Rownumbers AS
(
    SELECT 
        [Client code]
        ,Name
        ,Surname
        ,[Date of Birth]
        ,ROW_NUMBER() OVER
            (PARTITION BY 
                name
                ,surname
                ,[DATE of birth]
            ORDER BY 
                [client code]
            )AS [Row Number]

    FROM 
        kyc_details
)

SELECT 
,[client code]
,Name
,Surname
,[DATE of birth]
,[Row Number] 
FROM 
[Rownumbers]
WHERE
[Row Number] > 1

这里的问题是,我需要包括rownumber = 1。本质上,我需要选择具有重复值的所有列,但客户端代码将是唯一的。

sql sql-server tsql window-functions row-number
1个回答
2
投票

这是答案(我使用了分区的计数):

;WITH 
rownumbers AS 
(
    SELECT 
        [Client code],name, surname, [date of birth]
        ,COUNT(*) OVER (PARTITION BY  kd.name, kd.Surname,[DATE of birth]) AS total
FROM 
    kyc_details AS kd 

   )

SELECT 
,[client code]
,Name
,Surname
,[DATE of birth]
,total  
FROM 
rownumbers
WHERE 
total > 1   
ORDER BY 
Name
,surname    
© www.soinside.com 2019 - 2024. All rights reserved.