diff --git a/sql/reports/challenges/challenges-history.sql b/sql/reports/challenges/challenges-history.sql index aece5c9..c43416b 100644 --- a/sql/reports/challenges/challenges-history.sql +++ b/sql/reports/challenges/challenges-history.sql @@ -4,13 +4,21 @@ SELECT c.groups as "groupNames", c.status as "challengeStatus", c."registrationStartDate" as "registrationStartDate", - CASE WHEN c.status = 'COMPLETED' THEN - (SELECT cp."actualEndDate" FROM challenges."ChallengePhase" cp WHERE cp."challengeId"=c.id ORDER BY cp."scheduledEndDate" DESC LIMIT 1) - ELSE null END as "challengeCompletionDate", + CASE + WHEN c.status = 'COMPLETED' THEN latest_phase."actualEndDate" + ELSE null + END as "challengeCompletionDate", c.tags as "tags", c."projectId" as "projectId" FROM challenges."Challenge" c +LEFT JOIN LATERAL ( + SELECT cp."actualEndDate" + FROM challenges."ChallengePhase" cp + WHERE cp."challengeId" = c.id + ORDER BY cp."scheduledEndDate" DESC + LIMIT 1 +) latest_phase ON true LEFT JOIN challenges."ChallengeBilling" cb on cb."challengeId"=c.id -- filter by billing account @@ -19,40 +27,13 @@ WHERE ($1::text[] IS NULL OR cb."billingAccountId" = ANY($1::text[])) AND ($2::text[] IS NULL OR cb."billingAccountId" <> ANY($2::text[])) -- filter by challenge status AND ($3::text[] IS NULL OR c.status::text= ANY($3::text[])) - -- -- filter by completion date - -- filter by challengeCompletedDate - AND (c.status!='COMPLETED' OR ( + -- filter by completion date bounds on the latest challenge phase end date + AND ( ($4::timestamptz IS NULL AND $5::timestamptz IS NULL) OR ( - $5::timestamptz IS NULL AND ( - ( - SELECT cp."actualEndDate" - FROM challenges."ChallengePhase" cp - WHERE cp."challengeId" = c.id - ORDER BY cp."scheduledEndDate" DESC - LIMIT 1 - ) >= $4::timestamptz - ) + latest_phase."actualEndDate" IS NOT NULL + AND ($4::timestamptz IS NULL OR latest_phase."actualEndDate" >= $4::timestamptz) + AND ($5::timestamptz IS NULL OR latest_phase."actualEndDate" <= $5::timestamptz) ) - OR ( - $4::timestamptz IS NULL AND $5::timestamptz IS NOT NULL AND ( - ( - SELECT cp."actualEndDate" - FROM challenges."ChallengePhase" cp - WHERE cp."challengeId" = c.id - ORDER BY cp."scheduledEndDate" DESC - LIMIT 1 - ) >= $5::timestamptz - ) - ) - OR ( - $4::timestamptz IS NOT NULL AND $5::timestamptz IS NOT NULL AND (( - SELECT cp."actualEndDate" - FROM challenges."ChallengePhase" cp - WHERE cp."challengeId" = c.id - ORDER BY cp."scheduledEndDate" DESC - LIMIT 1 - ) BETWEEN $4::timestamptz AND $5::timestamptz) - ) - )) + ) LIMIT 1000; diff --git a/sql/reports/challenges/registrants-history.sql b/sql/reports/challenges/registrants-history.sql index 0ceb7ac..943e6c5 100644 --- a/sql/reports/challenges/registrants-history.sql +++ b/sql/reports/challenges/registrants-history.sql @@ -3,13 +3,21 @@ SELECT c.status as "challengeStatus", cw.handle as "winnerHandle", CASE WHEN sub.placement = 1 THEN true ELSE false END as "isWinner", - CASE WHEN c.status = 'COMPLETED' THEN - (SELECT cp."actualEndDate" FROM challenges."ChallengePhase" cp WHERE cp."challengeId"=c.id ORDER BY cp."scheduledEndDate" DESC LIMIT 1) - ELSE null END as "challengeCompletedDate", + CASE + WHEN c.status = 'COMPLETED' THEN latest_phase."actualEndDate" + ELSE null + END as "challengeCompletedDate", res."memberHandle" as "registrantHandle", ROUND(sub."finalScore", 2) as "registrantFinalScore" FROM challenges."Challenge" c +LEFT JOIN LATERAL ( + SELECT cp."actualEndDate" + FROM challenges."ChallengePhase" cp + WHERE cp."challengeId" = c.id + ORDER BY cp."scheduledEndDate" DESC + LIMIT 1 +) latest_phase ON true LEFT JOIN resources."Resource" res ON c.id = res."challengeId" LEFT JOIN @@ -27,40 +35,13 @@ WHERE rr.name = 'Submitter' AND ($2::text[] IS NULL OR cb."billingAccountId" <> ANY($2::text[])) -- filter by challenge status AND ($3::text[] IS NULL OR c.status::text= ANY($3::text[])) - -- -- filter by completion date - -- filter by challengeCompletedDate - AND (c.status!='COMPLETED' OR ( + -- filter by completion date bounds on the latest challenge phase end date + AND ( ($4::timestamptz IS NULL AND $5::timestamptz IS NULL) OR ( - $5::timestamptz IS NULL AND ( - ( - SELECT cp."actualEndDate" - FROM challenges."ChallengePhase" cp - WHERE cp."challengeId" = c.id - ORDER BY cp."scheduledEndDate" DESC - LIMIT 1 - ) >= $4::timestamptz - ) + latest_phase."actualEndDate" IS NOT NULL + AND ($4::timestamptz IS NULL OR latest_phase."actualEndDate" >= $4::timestamptz) + AND ($5::timestamptz IS NULL OR latest_phase."actualEndDate" <= $5::timestamptz) ) - OR ( - $4::timestamptz IS NULL AND $5::timestamptz IS NOT NULL AND ( - ( - SELECT cp."actualEndDate" - FROM challenges."ChallengePhase" cp - WHERE cp."challengeId" = c.id - ORDER BY cp."scheduledEndDate" DESC - LIMIT 1 - ) >= $5::timestamptz - ) - ) - OR ( - $4::timestamptz IS NOT NULL AND $5::timestamptz IS NOT NULL AND (( - SELECT cp."actualEndDate" - FROM challenges."ChallengePhase" cp - WHERE cp."challengeId" = c.id - ORDER BY cp."scheduledEndDate" DESC - LIMIT 1 - ) BETWEEN $4::timestamptz AND $5::timestamptz) - ) - )) + ) LIMIT 1000;