diff --git a/onprc_ehr/resources/queries/study/demographicsMostRecentWeight.query.xml b/onprc_ehr/resources/queries/study/demographicsMostRecentWeight.query.xml new file mode 100644 index 000000000..f6db19819 --- /dev/null +++ b/onprc_ehr/resources/queries/study/demographicsMostRecentWeight.query.xml @@ -0,0 +1,35 @@ + + + + + + + true + true + + + Current Weight (kg) + /query/executeQuery.view?schemaName=study& + query.queryName=Weight& + query.id~eq=${id} + + 0.### + + + DateTime + Weight Date + /query/executeQuery.view?schemaName=study& + query.queryName=Weight& + query.id~eq=${id}& + query.date~eq=${MostRecentWeightDate} + + + + true + + + MostRecentWeight +
+
+
+
diff --git a/onprc_ehr/resources/queries/study/demographicsMostRecentWeight.sql b/onprc_ehr/resources/queries/study/demographicsMostRecentWeight.sql new file mode 100644 index 000000000..cbea5887c --- /dev/null +++ b/onprc_ehr/resources/queries/study/demographicsMostRecentWeight.sql @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2010-2019 LabKey Corporation + * + * Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0 + */ +SELECT + +w.id, +w.MostRecentWeightDate, +timestampdiff('SQL_TSI_DAY', w.MostRecentWeightDate, now()) AS DaysSinceWeight, + +null as weightField, +--NOTE: we need to be careful in case duplicate weights are entered on the same time +cast(( + SELECT round(cast(AVG(w2.weight) as double), 3) AS _expr + FROM study.weight w2 + WHERE w2.qcstate.publicdata = true AND w.id=w2.id AND w.MostRecentWeightDate=w2.date +) as double) AS MostRecentWeight + +FROM ( +SELECT + w.Id AS Id, + max(w.date) AS MostRecentWeightDate + +FROM study.weight w +WHERE w.qcstate.publicdata = true and w.weight is not null +GROUP BY w.id +) w + +--NOTE: altered to a subselect to avoid duplicate entries from weights with identical time +-- --find the most recent weight associated with that date +-- LEFT JOIN study.weight T2 +-- ON (w.MostRecentWeightDate = t2.date AND w.Id = t2.Id) +-- +-- WHERE t2.qcstate.publicdata = true \ No newline at end of file