根据最后一行的值更新当前和所有后代的行值

时间:2019-02-13 23:24:06

标签: sql sql-server tsql

想要使用相同的WorkProcessOrderId(如果所有行的VersionType为'Revision')最后一行的值(具有“ Update”或“ Original”作为VersionType Column值)更新当前行和所有后代行的值

我能够基于VersionType列正确更新OriginalOrderId列值,但是如果VersionType列值为'Revision',则所有具有VersionType'Revision'的后代行都应根据最后一行的值使用相同的OriginalOrderId更新。

WITH RECURSE_HIERARCHY 
AS 
(
SELECT
o. OrderId AS RootOrderId
, ISNULL (NULLIF (o. EnterpriseRequestIdentifier, ''), '-MISSING-') AS IonTitle_ERN
, CAST (NULL AS INT) AS ParentOrderId
, o. OrderId AS OrderId
, o. OrderNumber
, 1 AS RootOrderId_Sequence
FROM iontitle. Order AS o (NOLOCK)
WHERE 1 = 1
AND o. ParentOrderId IS NULL
AND o. ODS_IsDeleted = 0
UNION ALL
SELECT 
p. RootOrderId 
, ISNULL (o. EnterpriseRequestIdentifier, p. IonTitle_ERN) AS IonTitle_ERN
, o. ParentOrderId
, o. OrderId
, o. OrderNumber
, p. RootOrderId_Sequence + 1 AS RootOrderId_Sequence
FROM iontitle. Order AS o (NOLOCK)
JOIN RECURSE_HIERARCHY AS p
ON p. OrderId = o. ParentOrderId
)
SELECT  
IIF ((ovt. FriendlyName = 'Upgrade' OR ovt. FriendlyName = 'Update'),owp. OrderId, IIF ((ovt. FriendlyName = 'Revision'), o. ParentOrderId, o. RootOrderId)) AS OrginalOrderId
, owp. OrderId AS WorkProcessOrderId
, owp. OrderWorkProcessId
, o. OrderNumber
, owp. OrderWorkProcessTypeId
, owp. OrderWorkProcessStatusId
, owp. CreatedOn
, owp. ModifiedOn
, owp. CompletedOn
, ISNULL (ovt. FriendlyName, 'Original')
FROM RECURSE_HIERARCHY o
        JOIN iontitle. OrderWorkProcess owp 
            ON owp. OrderId = O.  OrderId
        LEFT JOIN iontitle. OrderVersionReason ovr
            ON ovr. ODS_VersionOrderID = o.  OrderId
           AND ovr. ODS_IsDeleted = 0
        LEFT JOIN iontitle. Metadata_OrderVersionType ovt
            ON ovt. OrderVersionTypeId = ovr. VersionTypeId
           AND ovt. ODS_IsDeleted = 0

ActualResult

OrginalOrderId  WorkProcessOrderId  OrderWorkProcessId  OrderNumber VersionType
193209  193209  882173  EOR20180823-6159268 Original
193209  193209  882174  EOR20180823-6159268 Original
193209  193209  882175  EOR20180823-6159268 Original
193209  193209  882929  EOR20180823-6159268 Original
193209  193209  887274  EOR20180823-6159268 Original
193209  193209  887276  EOR20180823-6159268 Original
193209  193209  887275  EOR20180823-6159268 Original
193209  193209  945685  EOR20180823-6159268 Original
207505  207505  953563  EOR20180925-7328755 Update
207505  207505  953565  EOR20180925-7328755 Update
207505  207505  953564  EOR20180925-7328755 Update
207505  217398  1003602 EOR20180925-7328755 Revision
207505  217398  1003604 EOR20180925-7328755 Revision
207505  217398  1003603 EOR20180925-7328755 Revision
217398  217399  1003606 EOR20180925-7328755 Revision
217398  217399  1003608 EOR20180925-7328755 Revision
217398  217399  1003607 EOR20180925-7328755 Revision
217399  222452  1029114 EOR20180925-7328755 Revision
217399  222452  1029116 EOR20180925-7328755 Revision
217399  222452  1029115 EOR20180925-7328755 Revision
222452  228123  1058549 EOR20180925-7328755 Revision
222452  228123  1058551 EOR20180925-7328755 Revision
222452  228123  1058550 EOR20180925-7328755 Revision
228123  229371  1065386 EOR20180925-7328755 Revision
228123  229371  1065388 EOR20180925-7328755 Revision
228123  229371  1065387 EOR20180925-7328755 Revision

ExpectedResult

OrginalOrderId  WorkProcessOrderId  OrderWorkProcessId  OrderNumber VersionType
193209  193209  882173  EOR20180823-6159268 Original
193209  193209  882174  EOR20180823-6159268 Original
193209  193209  882175  EOR20180823-6159268 Original
193209  193209  882929  EOR20180823-6159268 Original
193209  193209  887274  EOR20180823-6159268 Original
193209  193209  887276  EOR20180823-6159268 Original
193209  193209  887275  EOR20180823-6159268 Original
193209  193209  945685  EOR20180823-6159268 Original
207505  207505  953563  EOR20180925-7328755 Update
207505  207505  953565  EOR20180925-7328755 Update
207505  207505  953564  EOR20180925-7328755 Update
207505  217398  1003602 EOR20180925-7328755 Revision
207505  217398  1003604 EOR20180925-7328755 Revision
207505  217398  1003603 EOR20180925-7328755 Revision
207505  217399  1003606 EOR20180925-7328755 Revision
207505  217399  1003608 EOR20180925-7328755 Revision
207505  217399  1003607 EOR20180925-7328755 Revision
207505  222452  1029114 EOR20180925-7328755 Revision
207505  222452  1029116 EOR20180925-7328755 Revision
207505  222452  1029115 EOR20180925-7328755 Revision
207505  228123  1058549 EOR20180925-7328755 Revision
207505  228123  1058551 EOR20180925-7328755 Revision
207505  228123  1058550 EOR20180925-7328755 Revision
207505  229371  1065386 EOR20180925-7328755 Revision
207505  229371  1065388 EOR20180925-7328755 Revision
207505  229371  1065387 EOR20180925-7328755 Revision

0 个答案:

没有答案