How to write T-SQL to extract duplicate composite key

jn93 671 Reputation points
2023-01-17T03:53:24.2966667+00:00

Hi All,

Lets say I have data with a composite key of CHDRNO,RSKNO. As per shown in example below, how can I write T-SQL code to extract the duplicate composite key of entire row from the entire table? Need help from expert on this because I need to extract the rows that consist of duplicate composite key for one of my company project.

   -- DDL and sample data population, start
   DECLARE @tbl TABLE (CHDRNO varchar(12), RSKNO int, ENDNLNE varchar(60),DATIME datetime2(7) )
   INSERT INTO @tbl (CHDRNO, RSKNO, ENDNLNE,DATIME) VALUES
   ('Z0012725','1','JUST','2020-08-03 14:45:02.7549260'),
   ('Z0012725','1','ABC','2020-08-04 14:45:02.7549260'),
   ('Z0012725','2','DEF','2020-08-03 14:45:02.7549260'),
   ('Z0012725','3','AND','2020-08-03 14:45:02.7549260')
   -- DDL and sample data population, end


User's image

Example and SQL Version

SQL version

SQL Server Integration Services
SQL Server Integration Services
A Microsoft platform for building enterprise-level data integration and data transformations solutions.
2,702 questions
SQL Server Other
0 comments No comments
{count} votes

Accepted answer
  1. Jingyang Li 5,896 Reputation points Volunteer Moderator
    2023-01-17T05:10:25.8166667+00:00

    --Or

    select CHDRNO, RSKNO, ENDNLNE,DATIME
    from @tbl t
    Where exists (select CHDRNO, RSKNO  from @tbl t1 
    where t.CHDRNO=t1.CHDRNO and t.RSKNO=t1.RSKNO
    group by  CHDRNO, RSKNO
    having (count(*)>1 )
    )
    

2 additional answers

Sort by: Most helpful
  1. Jingyang Li 5,896 Reputation points Volunteer Moderator
    2023-01-17T04:11:59.89+00:00

    Here you are:

    
       -- DDL and sample data population, start
       DECLARE @tbl TABLE (CHDRNO varchar(12), RSKNO int, ENDNLNE varchar(60),DATIME datetime2(7) )
       INSERT INTO @tbl (CHDRNO, RSKNO, ENDNLNE,DATIME) VALUES
       ('Z0012725','1','JUST','2020-08-03 14:45:02.7549260'),
       ('Z0012725','1','ABC','2020-08-04 14:45:02.7549260'),
       ('Z0012725','2','DEF','2020-08-03 14:45:02.7549260'),
       ('Z0012725','3','AND','2020-08-03 14:45:02.7549260')
    
    ;with mycte as (
    select *,count(*) over(partition by  CHDRNO,RSKNO) cnt from 
    @tbl) 
    
    select CHDRNO, RSKNO, ENDNLNE,DATIME from mycte
    where cnt>1
     
    
    0 comments No comments

  2. LiHongMSFT-4306 31,566 Reputation points
    2023-01-17T06:36:55.4033333+00:00

    Hi @jn93

    Try this query:

    SELECT A.* 
    FROM @tbl A JOIN (SELECT CHDRNO,RSKNO FROM @tbl GROUP BY CHDRNO,RSKNO HAVING COUNT(CHDRNO)>1)B 
      ON A.CHDRNO=B.CHDRNO AND A.RSKNO=B.RSKNO
    

    Best regards,

    Cosmog Hong


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.