SQL Server – Update Group Number Incrementally by 1 Based on Group

group bysql serversql-server-2012stored-procedurest-sql

I work on SQL server 2012 I face issue i can't update group no to every steps from 1 to 3
meaning i need every group from 1 to 3 take identity number incremental by 1 as 1,2,3 etc ..

create table #Replacement
       (
       PartIDC  INT,
       PartIDX  INT,
       FlagStatus nvarchar(50),
       HasReplacement nvarchar(50),
       groupId int,
       step tinyint
       )
    insert into #Replacement (PartIDC,PartIDX,FlagStatus,HasReplacement,groupId,step)
    values 
    
 (1222, 3421, 'Invalid', 'Critical', NULL, 1),
 (3421, 6421, 'Valid' ,    'Active' ,   NULL, 2),
 (1222, 6421, 'Valid' ,    'Chain',     NULL, 3),
 (5643, 2243, 'Invalid', 'Critical', NULL, 1),
 (2243, 3491, 'Valid' ,    'Active',   NULL, 2),
 (5643, 3491, 'Valid' ,    'Chain',     NULL, 3)

select * from #Replacement

Expected result

   PartIDC PartIDX FlagStatus HasReplacement groupId step
    1222 3421 Invalid           Critical      1      1 
    3421 6421 Valid              Active       1      2
    1222 6421 Valid           Chain           1      3
    5643 2243 Invalid           Critical      2      1
    2243 3491 Valid           Active          2      2
    5643 3491 Valid           Chain           2      3

always step 1 and step 3 are both equal on PartID C

always step 1 and step 2 are both equal on PartID x from step 1 equal to PartID C from step 2 .

so How to do Expected result above by update statement to group Id ?

Best Answer

The statement below will first create your groups based upon the PartIDC of those records with a Step = 1, and then use that for the subsequent steps. We then model the results and join to that to update the table:

 ;WITH 
 Step1 AS 
 (
     SELECT 
        PartIDC,
        PartIDX,
        FlagStatus,
        HasReplacement,
        groupId = ROW_NUMBER() OVER (ORDER BY PartIDC),
        step 
     FROM #Replacement WHERE step = 1
),
Step3 AS 
 (
    SELECT 
        r.PartIDC,
        r.PartIDX,
        r.FlagStatus,
        r.HasReplacement,
        s1.groupId,
        r.step 
    FROM #Replacement r JOIN Step1 s1 ON r.PartIDC = s1.PartIDc and r.Step =3
),
Step2 AS 
 (
    SELECT 
        r.PartIDC,
        r.PartIDX,
        r.FlagStatus,
        r.HasReplacement,
        s1.groupId,
        r.step 
    FROM #Replacement r 
        JOIN Step1 s1 ON r.PartIDC = s1.PartIDX and r.Step =2
),
NewValues AS 
(
    SELECT * FROM Step1  
    UNION ALL 
    SELECT * FROM STEP2
    UNION ALL 
    SELECT * FROM STEP3
)
UPDATE r
SET GroupID = nv.GroupID
FROM
    #Replacement r
        JOIN NewValues NV ON r.PartIDC = nv.PartIDC AND r.PartIDX = nv.PartIDX

This query:

SELECT * FROM #Replacement
ORDER BY GroupID, Step

Now produces:

PartIDC     PartIDX     FlagStatus                                         HasReplacement                                     groupId     step
----------- ----------- -------------------------------------------------- -------------------------------------------------- ----------- ----
1222        3421        Invalid                                            Critical                                           1           1
3421        6421        Valid                                              Active                                             1           2
1222        6421        Valid                                              Chain                                              1           3
5643        2243        Invalid                                            Critical                                           2           1
2243        3491        Valid                                              Active                                             2           2
5643        3491        Valid                                              Chain                                              2           3