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
Copy file name to clipboardExpand all lines: scripts/sql/export_email_addresses_for_incentives.sql
+4-2Lines changed: 4 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,5 @@
1
1
-- Commands are written on one line for psql execution, can be formatted for readability when running in a SQL client.
2
+
2
3
-- This script performs the following steps:
3
4
-- 1 - imports new data from csv file. The csv file is expected to be a full export of all partner records, but only new records will be inserted into the permanent table.
4
5
-- 2 - creates a list of email addresses of participants eligible for incentives
@@ -58,7 +60,7 @@ DROP TABLE IF EXISTS tmp_incentive_partner_staging;
58
60
-- Update PATH_TO_EXPORT_FILE before running.
59
61
\r
60
62
BEGIN;
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.idJOIN inhealth_partner_data ipd ONipd.nhs_number=cu.nhs_numberLEFT JOIN questions_incentivised qi ONqi.response_set_id=qrs.idWHEREipd.conducted_at>qrs.submitted_at::timestamptzANDqi.idIS NULLORDER BYqrs.id, qrs.submitted_atDESC;
63
+
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_numberASC, created_at DESC) SELECT DISTINCTON (cu.nhs_number) cu.idAS user_id, qrs.idAS response_set_id, cu.emailFROM canonical_users cu JOIN questions_responseset qrs ONqrs.user_id=cu.idJOIN inhealth_partner_data ipd ONipd.nhs_number=cu.nhs_numberWHEREipd.conducted_at>qrs.submitted_at::timestamptzANDNOT EXISTS (SELECT1FROM questions_incentivised qi JOIN questions_user iu ONiu.id=qi.user_idWHEREiu.nhs_number=cu.nhs_number) ORDER BYcu.nhs_numberASC, qrs.submitted_atDESC, qrs.idDESC;
62
64
\copy (SELECT email FROM tmp_eligible_incentive_export ORDER BY email) TO 'PATH_TO_EXPORT_FILE' WITH (FORMAT csv, HEADER true);
63
65
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;
64
66
SELECTcount(*) AS rows_exported_and_marked FROM tmp_eligible_incentive_export;
0 commit comments