how to eliminate duplicate column value in sql server?

Farshad Valizade 501 Reputation points
2023-10-21T05:32:39.3733333+00:00

hi every body

I have this welder list data and I want to show count of each column value and remove duplicate in it.

see this screenshot:

Capture

I want to have count of each welder in this result :

GTA1GTA2SMA1SMA22123HOW CAN i DO THAT?

SQL Server Other
{count} votes

2 answers

Sort by: Most helpful
  1. Viorel 122.6K Reputation points
    2023-10-21T07:11:07.4566667+00:00

    Check this query:

    select welder, count(distinct c) as [count]
    from MyTable
    cross apply (values  
        ('GTA1', GTA1), 
        ('GTA2', GTA2),
        ('SMA1', SMA1),
        ('SMA2', SMA2)
    ) w(welder, c)
    group by welder
    
    0 comments No comments

  2. LiHongMSFT-4306 31,566 Reputation points
    2023-10-23T01:41:34.55+00:00

    Hi @Farshad Valizade

    Please check this sample:

    Declare @temp table (id int, GTA1 varchar(20), GTA2 varchar(20), SMA1 varchar(20), SMA2 varchar(20));
    insert into @temp values
    (1,'002','001','003','004'),
    (2,'','001','',''),
    (3,'001','001','002','002'),
    (4,'','001','002','004')
    
    SELECT COUNT(DISTINCT NULLIF(GTA1,'')) AS GTA1,
           COUNT(DISTINCT NULLIF(GTA2,'')) AS GTA2,
           COUNT(DISTINCT NULLIF(SMA1,'')) AS SMA1,
           COUNT(DISTINCT NULLIF(SMA2,'')) AS SMA2
    FROM @temp
    

    Output:

    User's image

    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.