Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor sql query for mergeable patient retrieval #124

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions api/app/routers/entities_std.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ async def get_patientrecords_of_updated_patients(
total_amount = await conn.execute_query_dict(
f"""
select count(*) as quant
from std__patientrecord std
left join patient on patient.patient_code = std.patient_code
where std.created_at between '{start_datetime}' and '{end_datetime}'
and ((patient.id is null) or (patient.updated_at < std.created_at))
from(
select distinct std.patient_code
from std__patientrecord std
where std.created_at between '{start_datetime}' and '{end_datetime}'
) as tmp
"""
)
total_amount = total_amount[0]['quant']
Expand All @@ -71,14 +72,15 @@ async def get_patientrecords_of_updated_patients(
raw.source_updated_at as event_moment,
raw.updated_at as ingestion_moment
from std__patientrecord std
inner join public.raw__patientrecord raw on std.raw_source_id = raw.id
inner join datasource ds on raw.data_source_id = ds.cnes
inner join public.raw__patientrecord raw
on std.raw_source_id = raw.id
inner join datasource ds
on raw.data_source_id = ds.cnes
where std.patient_code in (
select std.patient_code
from std__patientrecord std
left join patient on patient.patient_code = std.patient_code
where std.created_at between '{start_datetime}' and '{end_datetime}'
and ((patient.id is null) or (patient.updated_at < std.created_at))
where std.created_at between
'{start_datetime}' and '{end_datetime}'
)
) tmp
group by tmp.patient_code
Expand Down
Loading