Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<query xmlns="http://labkey.org/data/xml/query">
<metadata>
<tables xmlns="http://labkey.org/data/xml">
<table tableName="demographicsMostRecentWeight" tableDbType="NOT_IN_DB">
<columns>
<column columnName="Id">
<isKeyField>true</isKeyField>
<isHidden>true</isHidden>
</column>
<column columnName="MostRecentWeight">
<columnTitle>Current Weight (kg)</columnTitle>
<url>/query/executeQuery.view?schemaName=study&amp;
query.queryName=Weight&amp;
query.id~eq=${id}
</url>
<formatString>0.###</formatString>
</column>
<column columnName="MostRecentWeightDate">
<formatString>DateTime</formatString>
<columnTitle>Weight Date</columnTitle>
<url>/query/executeQuery.view?schemaName=study&amp;
query.queryName=Weight&amp;
query.id~eq=${id}&amp;
query.date~eq=${MostRecentWeightDate}
</url>
</column>
<column columnName="weightField">
<isHidden>true</isHidden>
</column>
</columns>
<titleColumn>MostRecentWeight</titleColumn>
</table>
</tables>
</metadata>
</query>
35 changes: 35 additions & 0 deletions onprc_ehr/resources/queries/study/demographicsMostRecentWeight.sql
Original file line number Diff line number Diff line change
@@ -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
Loading