From f63ce8cfe053771e8d93fcda555301747fd225ac Mon Sep 17 00:00:00 2001 From: warren Date: Wed, 15 Jan 2025 15:32:30 +0000 Subject: [PATCH] fix: refactor SQL --- .../Database/CreateCohortDistributionData.cs | 47 ++++++++++--------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/application/CohortManager/src/Functions/Shared/Data/Database/CreateCohortDistributionData.cs b/application/CohortManager/src/Functions/Shared/Data/Database/CreateCohortDistributionData.cs index b0ea7f48b..477ae80e4 100644 --- a/application/CohortManager/src/Functions/Shared/Data/Database/CreateCohortDistributionData.cs +++ b/application/CohortManager/src/Functions/Shared/Data/Database/CreateCohortDistributionData.cs @@ -190,7 +190,8 @@ public List GetUnextractedCohortDistributionPa " bcd.[REQUEST_ID] " + " FROM [dbo].[BS_COHORT_DISTRIBUTION] bcd " + " WHERE bcd.IS_EXTRACTED = @Extracted " + - " AND REQUEST_ID IS NULL"; + " AND REQUEST_ID IS NULL " + + " ORDER BY bcd.RECORD_INSERT_DATETIME ASC "; var parameters = new Dictionary { @@ -484,37 +485,37 @@ private bool MarkCohortDistributionParticipantsAsExtracted(List $"@param{i}")); - - var SQL = $@" - WITH cte_latestParticipants AS ( - SELECT cd.PARTICIPANT_ID, cd.RECORD_UPDATE_DATETIME, - ROW_NUMBER() OVER(PARTITION BY PARTICIPANT_ID - ORDER BY RECORD_UPDATE_DATETIME DESC) as RowNum - FROM BS_COHORT_DISTRIBUTION cd - WHERE PARTICIPANT_ID IN ({cohortParamList}) - AND IS_EXTRACTED = 0 - AND REQUEST_ID IS NULL) + var SQL = $@" WITH AllUnextractedParticipants AS ( + SELECT + PARTICIPANT_ID, + RECORD_INSERT_DATETIME, + ROW_NUMBER() OVER ( + ORDER BY RECORD_INSERT_DATETIME ASC + ) AS OverallRowNum + FROM BS_COHORT_DISTRIBUTION + WHERE IS_EXTRACTED = 0 + AND REQUEST_ID IS NULL), + FilteredCohortDistribution AS ( + SELECT TOP (@RowCount) + PARTICIPANT_ID, + RECORD_INSERT_DATETIME + FROM AllUnextractedParticipants + ORDER BY OverallRowNum) UPDATE cd SET IS_EXTRACTED = @Extracted, REQUEST_ID = @RequestId FROM BS_COHORT_DISTRIBUTION cd - INNER JOIN cte_latestParticipants cte - ON cd.PARTICIPANT_ID = cte.PARTICIPANT_ID - AND cd.RECORD_UPDATE_DATETIME = cte.RECORD_UPDATE_DATETIME - WHERE cte.RowNum = 1"; + INNER JOIN FilteredCohortDistribution fcd + ON cd.PARTICIPANT_ID = fcd.PARTICIPANT_ID + AND cd.RECORD_INSERT_DATETIME = fcd.RECORD_INSERT_DATETIME"; var parameters = new Dictionary { - {"@Extracted", 1 }, - {"@RequestId", requestId } + { "@Extracted", 1 }, + { "@RequestId", requestId }, + { "@RowCount", cohortParticipants.Count } }; - for (int i = 0; i < cohortParticipants.Count; i++) - { - parameters.Add($"@param{i}", cohortParticipants[i].ParticipantId); - } - var sqlToExecute = new List { new SQLReturnModel