Sql-Server

如何根據從 1 到 3 的每個組將組無增量更新 1?

  • September 14, 2020

我在 SQL Server 2012 上工作我面臨的問題我無法將組號更新到從 1 到 3 的每個步驟,這意味著我需要從 1 到 3 的每個組將標識號遞增 1 作為 1、2、3 等。

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)

從#Replacement中選擇*

預期結果

  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

在 PartID C 上,第 1 步和第 3 步總是相等

總是 step 1 和 step 2 在 PartID x 來自 step 1 等於 PartID C 來自 step 2 。

那麼如何通過更新語句對組 ID 執行上述預期結果?

下面的語句將首先根據這些記錄的 PartIDC 創建您的組Step = 1,然後將其用於後續步驟。然後我們對結果進行建模並加入其中以更新表:

;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

這個查詢:

SELECT * FROM #Replacement
ORDER BY GroupID, Step

現在產生:

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

引用自:https://dba.stackexchange.com/questions/275447