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:
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.