Skip to content

Commit

Permalink
fix: try re-writing the query
Browse files Browse the repository at this point in the history
  • Loading branch information
njogz committed Feb 7, 2024
1 parent 8e72174 commit b260ec2
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions models/brac/contact_views/contactview_branch.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,25 @@
}}

SELECT
contactview_hospital.uuid,
contactview_hospital.name,
couchdb.doc->>'area' AS area,
couchdb.doc->>'region' AS region,
"@timestamp"::timestamp without time zone AS "@timestamp"
FROM
{{ ref("contactview_hospital") }}
INNER JOIN {{ ref("couchdb") }} ON (couchdb.doc ->> '_id'::text = contactview_hospital.uuid AND couchdb.doc ->> 'type' = 'district_hospital')

uuid,
name,
area,
region,
"timestamp"
FROM (
SELECT
contactview_hospital.uuid,
contactview_hospital.name,
couchdb.doc->>'area' AS area,
couchdb.doc->>'region' AS region,
"@timestamp"::timestamp without time zone AS "timestamp",
ROW_NUMBER() OVER (PARTITION BY contactview_hospital.uuid ORDER BY contactview_hospital."@timestamp" DESC) AS row_num
FROM
{{ ref("contactview_hospital") }} contactview_hospital
INNER JOIN {{ ref("couchdb") }} couchdb ON (couchdb.doc ->> '_id' = contactview_hospital.uuid AND couchdb.doc ->> 'type' = 'district_hospital')
) AS subquery
WHERE
row_num = 1
{% if is_incremental() %}
WHERE contactview_branch."@timestamp" > {{ max_existing_timestamp('"@timestamp"', target_ref=ref("couchdb")) }}
{% else %}
WHERE 1=1
{% endif %}
AND "timestamp" > {{ max_existing_timestamp('"@timestamp"', target_ref=ref("couchdb")) }}
{% endif %}

0 comments on commit b260ec2

Please sign in to comment.