You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-- INNER JOIN table_names ON LTRIM(table_names.DATA_STORE_CODE) = OMD_DATA_STORE.DATA_STORE_CODE
66
-
-- INNER JOIN dbo.OMD_MODULE_DATA_STORE ON OMD_MODULE_DATA_STORE.DATA_STORE_ID = OMD_DATA_STORE.DATA_STORE_ID
67
-
-- )
68
74
) sub
69
75
WHERE ROW_ORDER=1
70
-
), ELIGIBLE_WINDOWS AS
76
+
)
77
+
, ELIGIBLE_WINDOWS AS
71
78
(
72
79
SELECT
73
80
SUM(CHANGE_DELTA_INDICATOR) AS ELIGIBILITY_COUNT,
81
+
(SELECTCOUNT (DISTINCT DATA_OBJECT_TARGET) FROM ConsistencyCTE) AS MODULE_TABLE_COUNT,
74
82
MAX(INTERVAL_END_DATETIME) AS MAX_END_DATETIME,
75
-
MAX(MODULE_INSTANCE_ID) AS MAX_MODULE_INSTANCE_ID
83
+
MAX(MODULE_INSTANCE_ID) AS MAX_MODULE_INSTANCE_ID,
84
+
(SELECTCOUNT(*) FROM table_names) AS TABLE_COUNT
76
85
FROM ConsistencyCTE
77
86
)
78
-
SELECT@Output =
79
-
MIN
87
+
SELECT
88
+
@Output =MIN
80
89
(CASE
81
-
WHEN ELIGIBILITY_COUNT =0THEN MAX_END_DATETIME
82
-
WHEN ELIGIBILITY_COUNT !=0AND CHANGE_DELTA_INDICATOR =1THEN INTERVAL_END_DATETIME
90
+
WHEN MODULE_TABLE_COUNT < TABLE_COUNT THENNULL-- Not all tables have been hit with records yet, only relevant when starting from scratch / drop everything.
91
+
WHEN ELIGIBILITY_COUNT =0THEN MAX_END_DATETIME -- If the eligibility count is 0 then MAX(INTERVAL_END_DATETIME), because there are no changes at all to be detected.
92
+
WHEN ELIGIBILITY_COUNT !=0AND CHANGE_DELTA_INDICATOR =1THEN INTERVAL_END_DATETIME-- If the eligbility count != 0 then remove all rows where START_DATETIME=END_DATETIME and select the MIN(INTERVAL_END_DATETIME) from this result
83
93
END)
84
-
--MIN(CASE
85
-
-- WHEN ELIGIBILITY_COUNT = 0 THEN MAX_MODULE_INSTANCE_ID
86
-
-- WHEN ELIGIBILITY_COUNT != 0 AND CHANGE_DELTA_INDICATOR = 1 THEN MODULE_INSTANCE_ID
87
-
--END) AS CONSISTENCY_MODULE_INSTANCE_ID
88
94
FROM ConsistencyCTE
89
95
CROSSJOIN ELIGIBLE_WINDOWS
90
-
-- If the eligibility count is 0 then MAX(INTERVAL_END_DATETIME)
91
-
-- If the eligbility count != 0 then remove all rows where START_DATETIME=END_DATETIME and select the MIN(INTERVAL_END_DATETIME) from this result
0 commit comments