SQL Server: How to print original & duplicate data

T.Zacks 3,986 Reputation points
2021-12-03T16:40:42.69+00:00

Below sql print which data has duplicate but i want the output bit different way

I want to print the actual data and duplicate too. in the below sql here data is group by masterid,SectionID, LineItemID,PeriodID but their value field could have different data.

so tell me how could i print actual & duplicate too in same result one after one. if my question not clear then please let me know so i will try to elaborate it with example. thanks

WITH CTE AS
(
    SELECT *,ROW_NUMBER() OVER (PARTITION BY masterid,SectionID, LineItemID,PeriodID ORDER BY masterid,SectionID, LineItemID,PeriodID) AS RN
    FROM tblTicker_Bogey 
)

Select * from CTE where RN>1
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,558 questions
{count} votes

1 additional answer

Sort by: Most helpful
  1. Erland Sommarskog 101.8K Reputation points MVP
    2021-12-03T22:49:55.717+00:00

    If I understand it correctly, this is what you're after:

    WITH CTE AS    (
          SELECT *, COUNT() OVER (PARTITION BY masterid,SectionID, LineItemID,PeriodID) AS cnt
          FROM tblTicker_Bogey 
    )
    Select * from CTE where cnt>1
    
    1 person found this answer helpful.
    0 comments No comments