how to get number of duplicate value in sql?

Farshad Valizade 501 Reputation points
2023-07-04T05:27:56.19+00:00

hi guys.

I have a table for keep history of a piping result. a pipe should weld and after weld I save the result of that weld : Acc Rej or some thing else.

Weld Result for a pipe :

  • step 1 : repair
  • step 2: repair
  • step3:repair
  • step4 :acc

now I want to query this history and get this result: how can I do that.

Capture

weldID   Status 
1431     RP3/ACC1

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
14,284 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 118.9K Reputation points
    2023-07-04T06:51:20.1166667+00:00

    If the names are not fixed, then check this query too:

    select WeldId,
        (
            select string_agg(c, '/') within group (order by s)
            from
            (
                select min(NdtStep) s, concat(ShortName, count(*)) as c
                from MyTable
                where WeldId = t.WeldId
                group by ShortName
            ) g
        )
    from MyTable t
    group by WeldId
    

    If STRING_AGG is not available on your server, it can be replaced with another construct.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. PercyTang-MSFT 12,506 Reputation points Microsoft Vendor
    2023-07-04T06:05:52.1066667+00:00

    Hi @Farshad Valizade

    You can try this query.

    create table test(ResultId int,RequestId int,WeldId int,NdtStatusId int,NdtStep int,NdtVold int,
    RepRemark varchar(10),NdtType varchar(20),NdtStatus varchar(20),ShortName varchar(10));
    insert into test values
    (2331,2225,1431,5,1,1,null,'RT','Repair','RP'),
    (2333,2225,1431,5,3,1,null,'RT','Repair','RP'),
    (2334,2225,1431,1,4,0,null,'RT','ACC','A'),
    (2482,2225,1431,5,2,1,null,'RT','Repair','RP')
    
    ;with T1 as(
      select WeldId,ShortName,count(*) as nums from test group by WeldId,ShortName
    ),T2 as(
      select WeldId,case when ShortName = 'A' then 'ACC' else ShortName end as ShortName,nums 
      from T1
    ),T3 as(
      select WeldId,cast(ShortName as varchar) + cast(nums as varchar) as t from T2
    ),T4 as(
      select WeldId,case when left(t,1) = 'A' then t else null end as m,
      case when left(t,1) = 'R' then t else null end as n from T3
    ),T5 as(
      select WeldId,max(m) as m,max(n) as n from T4 group by WeldId)
    select WeldId,n + '/' + m as Status from T5;
    

    Output:

    User's image

    Best regards,

    Percy Tang


    If the answer is the right solution, please click "Accept Answer". 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.