Skip to content
Open
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
13 changes: 8 additions & 5 deletions onprc_ehr/resources/queries/study/pregnancyGestation.sql
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,19 @@ SELECT
m.id,
m.date,
m.gestation_days as gestation_days,
TIMESTAMPADD('SQL_TSI_DAY',(p.Gestation - m.gestation_days), m.date) as ExpectedDelivery,
CASE
WHEN m.gestation_days is not null then
TIMESTAMPADD('SQL_TSI_DAY',(p.Gestation - m.gestation_days), m.date)
WHEN m.estDeliverydate is not null then m.estDeliverydate
ELSE
'None'
END as ExpectedDelivery,
m.QCState

FROM study.pregnancyConfirmation m
INNER JOIN ehr_lookups.species p on (m.Id.DataSet.demographics.species = p.common )
And m.date in (select max(s.date) AS d from study.pregnancyConfirmation s where s.id = m.id)
And m.Id.DataSet.demographics.calculated_status.code = 'Alive'
And p.Gestation is not null
And (m.outcome.birthDate >= cast(now() as date) or m.outcome.birthDate is null)
And (Select count(*) from ehr.snomed_tags stg where stg.code.code = 'F-30980' And stg.id = m.id
And stg.date >= m.date ) = 0
And m.gestation_days is not null