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
DROPTABLE IF EXISTS tmp_incentive_partner_staging;
54
55
55
56
56
57
-- TRANSACTION START for exporting eligible participants for incentives and updating incentivised table.
57
58
-- Update PATH_TO_EXPORT_FILE before running.
58
59
\r
59
60
BEGIN;
60
-
CREATE TEMP TABLE tmp_eligible_incentive_export AS WITH canonical_users AS (SELECT DISTINCTON (nhs_number) id, email, nhs_number FROM questions_user WHERE nhs_number IS NOT NULLORDER BY nhs_number, created_at DESC) SELECT DISTINCTON (qrs.id) cu.idAS user_id, qrs.idAS response_set_id, cu.emailFROM canonical_users cu JOIN questions_responseset qrs ONqrs.user_id=cu.idJOINincentive_partner_import ipiONipi.nhs_number=cu.nhs_numberLEFT JOIN questions_incentivised qi ONqi.response_set_id=qrs.idWHEREipi.conducted_at>qrs.submitted_at::timestamptzANDqi.id IS NULLORDER BYqrs.id, qrs.submitted_atDESC;
61
+
CREATE TEMP TABLE tmp_eligible_incentive_export AS WITH canonical_users AS (SELECT DISTINCTON (nhs_number) id, email, nhs_number FROM questions_user WHERE nhs_number IS NOT NULLORDER BY nhs_number, created_at DESC) SELECT DISTINCTON (qrs.id) cu.idAS user_id, qrs.idAS response_set_id, cu.emailFROM canonical_users cu JOIN questions_responseset qrs ONqrs.user_id=cu.idJOINinhealth_partner_data ipdONipd.nhs_number=cu.nhs_numberLEFT JOIN questions_incentivised qi ONqi.response_set_id=qrs.idWHEREipd.conducted_at>qrs.submitted_at::timestamptzANDqi.id IS NULLORDER BYqrs.id, qrs.submitted_atDESC;
61
62
\copy (SELECT email FROM tmp_eligible_incentive_export ORDER BY email) TO 'PATH_TO_EXPORT_FILE' WITH (FORMAT csv, HEADER true);
62
63
INSERT INTO questions_incentivised (created_at, updated_at, incentivised_at, user_id, response_set_id) SELECT now(), now(), now(), user_id, response_set_id FROM tmp_eligible_incentive_export;
63
64
SELECTcount(*) AS rows_exported_and_marked FROM tmp_eligible_incentive_export;
64
65
65
-
--I happy with the Select result type COMMIT; if not, ROLLBACK; to undo changes;
66
+
--If happy with the Select result type COMMIT; if not, ROLLBACK; to undo changes;
66
67
67
68
-- COMMIT;
68
69
-- ROLLBACK;
69
70
70
71
-- TRANSACTION END
71
72
72
-
-- DELETE temp table
73
+
-- DELETE tempprary export table
73
74
DROPTABLE IF EXISTS tmp_eligible_incentive_export;
0 commit comments