Share via

Duplicated name -

Will Faulkner 201 Reputation points
2020-10-13T06:39:33.93+00:00

Hello

In a simple query:

SELECT REFNO, FIRSTNAME, LASTNAME
FROM TABLE
WHERE...

I often see duplicated names. So for example the output can be:

REFNO FIRSTNAME LASTNAME

1 BILL FAULKNER
2 BILL FAULKNER

I would like to write a report that highlights these duplicate entries but which keeps the output in one row,

So the desired output would be:

REFNO1 REFNO2 FIRSTNAME LASTNAME
1 2 BILL FAULKNER

Please can you advise how I'd go about achieving this?

With many thanks.

Developer technologies | Transact-SQL
Developer technologies | Transact-SQL

A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.

0 comments No comments

Answer accepted by question author

EchoLiu-MSFT 14,626 Reputation points
2020-10-13T06:59:58.733+00:00

Hi @Will Faulkner

Please refer to:

declare @test table (REFNO int, FIRSTNAME char(15),  LASTNAME char(15))  
INSERT INTO @test values(1,'BILL','FAULKNER'),(2 ,'BILL', 'FAULKNER')  
  
SELECT FIRSTNAME, LASTNAME  
       , STUFF((SELECT ',' + CAST(REFNO AS VARCHAR(30)) AS [text()]  
          FROM @test AS O  
          WHERE O.FIRSTNAME = C.FIRSTNAME and O.LASTNAME = C.LASTNAME  
          FOR XML PATH('')), 1, 1, NULL) AS REFNO  
FROM @test  AS c  
GROUP BY FIRSTNAME, LASTNAME  

31933-image.png

Regards
Echo


If the answer is helpful, please click "Accept Answer" and upvote it.
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.

Was this answer helpful?


4 additional answers

Sort by: Most helpful
  1. Viorel 127K Reputation points
    2020-10-13T07:20:03.04+00:00

    If there are no more than two duplicate names in each case, then, check this query too:

    select t1.REFNO as REFNO1, t2.REFNO as REFNO2, t1.FIRSTNAME, t2.LASTNAME
    from MyTable as t1 
    inner join MyTable as t2 on t2.FIRSTNAME = t1.FIRSTNAME and t2.LASTNAME = t1.LASTNAME and t2.REFNO > t1.REFNO
    order by t1.REFNO
    

    Was this answer helpful?

    0 comments No comments

  2. EchoLiu-MSFT 14,626 Reputation points
    2020-10-13T07:15:36.063+00:00

    Hi @Will Faulkner

    Please check another method:

        declare @test table (REFNO int, FIRSTNAME char(15),  LASTNAME char(15))  
        INSERT INTO @test values(1,'BILL','FAULKNER'),(2 ,'BILL', 'FAULKNER')  
          
        ;with cte  
        as(SELECT *,row_number() over(partition by FIRSTNAME,LASTNAME order by REFNO)rn from @test)  
        select c1.REFNO REFNO1,c2.REFNO REFNO2,c1.FIRSTNAME,c1.LASTNAME from cte c1  
        join cte c2 on c1.rn=c2.rn-1 and c1.FIRSTNAME=c2.FIRSTNAME and c1.LASTNAME=c2.LASTNAME  
    

    31953-image.png

    If you have any question, please feel free to let me know.
    If the response is helpful, please click "Accept Answer" and upvote it.

    Regards
    Echo


    If the answer is helpful, please click "Accept Answer" and upvote it.
    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.

    Was this answer helpful?


  3. Vaibhav Chaudhari 39,011 Reputation points Volunteer Moderator
    2020-10-13T06:49:52.837+00:00

    Does below query work?

    CREATE TABLE Mytable
        ([REFNO] int, [FIRSTNAME] varchar(4), [LASTNAME] varchar(8))
    ;
    
    INSERT INTO Mytable
        ([REFNO], [FIRSTNAME], [LASTNAME])
    VALUES
        (1, 'BILL', 'FAULKNER'),
        (2, 'BILL', 'FAULKNER'),
        (3, 'Joey', 'Bing'),
        (4, 'Joey', 'Bing')
    ;
    
    select STRING_AGG(RefNo,',') NewColumn,FIRSTNAME, LASTNAME from Mytable
    group by FIRSTNAME, LASTNAME
    

    Please don't forget to Accept Answer and Up-vote if the response helped -- Vaibhav

    Was this answer helpful?


  4. tibor_karaszi@hotmail.com 4,321 Reputation points
    2020-10-13T06:46:35.32+00:00

    What version of SQL Server are you on? STRING_AGG() might be used, but that was introduced in 2016, I believe.

    Was this answer helpful?


Your answer

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