In MSSQL Server, partition by is very useful.You can group the columns and give new values.
MSSQL Server partition by
In this situation, Customer name and Payment date must be grouping so duplicate values are findable.
Customer table and payment table join code :
select c.Name,p.PaymentDate,p.PaymentValue from dbo.Customer c inner join dbo.Payment p on c.CustomerId = p.CustomerId order by 1,2
Customer table and Payment table example values:
Query for finding duplicate values:
;WITH CTE AS ( SELECT c.Name,p.PaymentDate,p.PaymentValue,ROW_NUMBER() OVER (PARTITION BY c.Name,p.PaymentValue ORDER BY p.PaymentDate asc) AS [rn] from dbo.Customer c inner join dbo.Payment p on c.CustomerId = p.CustomerId ) select * from cte where rn>1
Query Result:
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…