diff --git a/onprc_ehr/resources/queries/study/pregnancyGestation.sql b/onprc_ehr/resources/queries/study/pregnancyGestation.sql index 5fbcd63eb..145072893 100644 --- a/onprc_ehr/resources/queries/study/pregnancyGestation.sql +++ b/onprc_ehr/resources/queries/study/pregnancyGestation.sql @@ -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 +