Skip to content
Open
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,9 @@
<query xmlns="http://labkey.org/data/xml/query">
<metadata>
<tables xmlns="http://labkey.org/data/xml">
<table tableName="Demographics_NotInMMA" tableDbType="NOT_IN_DB">
<tableTitle>Demographics (Excluding animals in Weight MMA regimen)</tableTitle>
</table>
</tables>
</metadata>
</query>
54 changes: 54 additions & 0 deletions onprc_ehr/resources/queries/study/Demographics_NotInMMA.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
Created by Kollil in Dec 2025
Tkt # 13461
Added two filters to the Demographics dataset:
1. Filter out any animal with the following SNOMED Codes:
Begin active weight management regimen (P-YY961)
However, we would need to include animals that have this additional SNOMED Code if it's entered AFTER the one above
Release from active weight management regimen (P-YY960)
2. Remove Shelters, Corral and Hospital locations from the lists
*/

SELECT
d.Id.curlocation.area AS Area,
d.Id.curlocation.room AS Room,
d.Id.curlocation.cage AS Cage,
d.Id,
d.Id.utilization.use AS ProjectsAndGroups,
d.species,
d.geographic_origin,
d.gender AS Sex,
d.calculated_status,
d.birth,
d.Id.Age.YearAndDays,
d.Id.MostRecentWeight.MostRecentWeight,
d.Id.MostRecentWeight.MostRecentWeightDate,
d.Id.viral_status.viralStatus,
d.history
FROM Demographics d
WHERE d.Id.curlocation.area NOT IN ('Shelters', 'Corral', 'Hospital')-- Exclude animals from these locations
AND NOT (-- Exclude females under 5yrs, males under 7yrs
(d.gender.code = 'f' AND d.Id.age.ageInYears < 5)
OR (d.gender.code = 'm' AND d.Id.age.ageInYears < 7)
)
AND NOT EXISTS (
-- -- Find animals whose latest 'Weight MMA BEGIN' has no later 'Weight MMA RELEASE'
SELECT 1
FROM study.WeightManagementMMAData b
WHERE b.Id = d.Id
AND b.code = 'P-YY961'
AND b.date = (
SELECT MAX(b2.date)
FROM study.WeightManagementMMAData b2
WHERE b2.Id = d.Id
AND b2.code = 'P-YY961'
)
AND NOT EXISTS (
SELECT 1
FROM study.WeightManagementMMAData r
WHERE r.Id = d.Id
AND r.code = 'P-YY960'
AND r.date > b.date
)
)

Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<query xmlns="http://labkey.org/data/xml/query">
<metadata>
<tables xmlns="http://labkey.org/data/xml">
<table tableName="weightPctChange" tableDbType="NOT_IN_DB">
<tableTitle>Weight Change, Relative to Previous Weight (Excluding the animals enrolled in MMA)</tableTitle>
<description>This query shows the percent change of each weight, relative to the weight immediately prior to it</description>
<columns>
<column columnName="lsid">
<isKeyField>true</isKeyField>
<isHidden>true</isHidden>
</column>
<column columnName="CurWeight">
<url>/query/executeQuery.view?schemaName=study&amp;
query.queryName=weight&amp;
query.id~eq=${id}&amp;
query.sort=-date
</url>
</column>
<column columnName="PrevWeight">
<url>/query/executeQuery.view?schemaName=study&amp;
query.queryName=weight&amp;
query.date~eq=${PrevDate}&amp;
query.sort=-date
</url>
</column>
<column columnName="PctChange">
<url>/query/executeQuery.view?schemaName=study&amp;
query.queryName=weight&amp;
query.lsid~eq=${lsid}&amp;
</url>
<columnTitle>Percent Change</columnTitle>
</column>
<column columnName="Interval">
<columnTitle>Interval (Days)</columnTitle>
</column>
<column columnName="LatestWeight">
<columnTitle>Current Weight (kg)</columnTitle>
</column>
<column columnName="LatestWeightDate">
<columnTitle>Weight Date</columnTitle>
</column>
<!--<column columnName="qcstate">-->
<!--<isHidden>true</isHidden>-->
<!--</column>-->
</columns>
<titleColumn>PctChange</titleColumn>
</table>
</tables>
</metadata>
</query>
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright (c) 2013-2019 LabKey Corporation
*
* Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0
*/
--this query contains a handful of calculated fields for the weights table
--it is designed to be joined into weights using lsid

SELECT
w.lsid,
w.Id,
w.date,
w.weight AS curWeight,
pd1.PrevDate as prevDate1,
pw1.weight AS prevWeight1,
Round(((w.weight - pw1.weight) * 100 / w.weight), 1) AS pctChange1,
timestampdiff('SQL_TSI_DAY', pw1.date, w.date) AS interval1,
pd2.PrevDate as PrevDate2,
pw2.weight AS PrevWeight2,
Round(((pw1.weight - pw2.weight) * 100 / pw1.weight), 1) AS PctChange2,
timestampdiff('SQL_TSI_DAY', pw2.date, pw1.date) AS Interval2
FROM study.weight w
--Find the next most recent weight date before this one
JOIN
(SELECT T2.Id, T2.date, max(T1.date) as PrevDate
FROM study.weight T1
JOIN study.weight T2 ON (T1.Id = T2.Id AND T1.date < T2.date)
WHERE t1.qcstate.publicdata = true AND t2.qcstate.publicdata = true
GROUP BY T2.Id, T2.date) pd1
ON (w.Id = pd1.Id AND w.date = pd1.Date)

--and the weight associated with that date
JOIN study.weight pw1
ON (w.Id = pw1.Id AND pw1.date = pd1.prevdate AND pw1.qcstate.publicdata = true)

--then find the next most recent date
LEFT JOIN
(SELECT T2.Id, T2.date, max(T1.date) as PrevDate
FROM study.weight T1
JOIN study.weight T2 ON (T1.Id = T2.Id AND T1.date < T2.date)
WHERE t1.qcstate.publicdata = true AND t2.qcstate.publicdata = true
GROUP BY T2.Id, T2.date
) pd2
ON (pd1.Id = pd2.Id AND pd1.PrevDate = pd2.date)

--and the weight associated with that date
JOIN study.weight pw2
ON (w.Id = pw2.Id AND pw2.date = pd2.prevdate AND pw2.qcstate.publicdata = true)
WHERE
w.qcstate.publicdata = true
AND pd1.date is not null
AND pd2.date is not null
--only include drops
AND w.weight < pw1.weight
AND pw1.weight < pw2.weight
AND ((w.weight - pw2.weight) * 100 / w.weight) < -3.0

AND w.Id.curlocation.area NOT IN ('Shelters', 'Corral', 'Hospital')-- Exclude animals from these locations
AND NOT (-- Exclude females under 5yrs, males under 7yrs
(w.Id.demographics.gender.code = 'f' AND w.Id.age.ageInYears < 5)
OR (w.Id.demographics.gender.code = 'm' AND w.Id.age.ageInYears < 7)
)
AND w.Id NOT IN (
SELECT 1 -- Find animals whose latest 'Weight MMA BEGIN' has no later 'Weight MMA RELEASE'
FROM study.WeightManagementMMAData b
WHERE b.Id = w.Id
AND b.code = 'P-YY961'
AND b.date = (
SELECT MAX(b2.date)
FROM study.WeightManagementMMAData b2
WHERE b2.Id = w.Id
AND b2.code = 'P-YY961'
)
AND NOT EXISTS (
SELECT 1
FROM study.WeightManagementMMAData r
WHERE r.Id = w.Id
AND r.code = 'P-YY960'
AND r.date > b.date
)
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<query xmlns="http://labkey.org/data/xml/query">
<metadata>
<tables xmlns="http://labkey.org/data/xml">
<table tableName="weightRelChange_NotInMMA" tableDbType="NOT_IN_DB">
<tableTitle>Weight Change, Relative to Current Weight (Excluding the animals enrolled in MMA)</tableTitle>
<description>This query shows the percent change of each weight, relative to the current weight</description>
<columns>
<column columnName="lsid">
<isKeyField>true</isKeyField>
<isHidden>true</isHidden>
</column>
<column columnName="Id">
<isHidden>true</isHidden>
<fk>
<fkDbSchema>study</fkDbSchema>
<fkTable>animal</fkTable>
<fkColumnName>id</fkColumnName>
</fk>
</column>
<column columnName="date">
<isHidden>true</isHidden>
<columnTitle>Date of Last Weight</columnTitle>
</column>
<column columnName="weight">
<columnTitle>Old Weight (kg)</columnTitle>
</column>
<column columnName="PctChange">
<columnTitle>% Change Relative To Current</columnTitle>
<conditionalFormats>
<conditionalFormat>
<filters>
<filter operator="lte" value="-10"/>
</filters>
<bold>true</bold>
<backgroundColor>FF0000</backgroundColor>
</conditionalFormat>
<conditionalFormat>
<filters>
<filter operator="gte" value="10"/>
</filters>
<bold>true</bold>
<backgroundColor>458B00</backgroundColor>
</conditionalFormat>
</conditionalFormats>
</column>
<column columnName="IntervalInDays">
<columnTitle>Days Since Weight</columnTitle>
</column>
<column columnName="IntervalInMonths">
<columnTitle>Months Since Weight</columnTitle>
</column>
<column columnName="LatestWeight">
<columnTitle>Latest Weight (kg)</columnTitle>
<url>/query/executeQuery.view?schemaName=study&amp;
query.queryName=weight&amp;
query.date~eq=${LatestWeightDate}&amp;
query.sort=-date
</url>
</column>
<column columnName="LatestWeightDate">
<columnTitle>Latest Weight Date</columnTitle>
</column>
</columns>
<titleColumn>PctChange</titleColumn>
</table>
</tables>
</metadata>
</query>
62 changes: 62 additions & 0 deletions onprc_ehr/resources/queries/study/weightRelChange_NotInMMA.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
Created by Kollil in Dec 2025
Tkt # 13461
Added two filters to the Demographics dataset:
1. Filter out any animal with the following SNOMED Codes:
Begin active weight management regimen (P-YY961)
However, we would need to include animals that have this additional SNOMED Code if it's entered AFTER the one above
Release from active weight management regimen (P-YY960)
2. Remove Shelters, Corral and Hospital locations from the lists
*/

SELECT
w.lsid,
w.Id,
w.date,
w.Id.MostRecentWeight.MostRecentWeightDate as LatestWeightDate,
w.Id.MostRecentWeight.MostRecentWeight AS LatestWeight,

timestampdiff('SQL_TSI_DAY', w.date, w.Id.MostRecentWeight.MostRecentWeightDate) AS IntervalInDays,
age_in_months(w.date, w.Id.MostRecentWeight.MostRecentWeightDate) AS IntervalInMonths,

w.weight,
CASE WHEN w.date >= timestampadd('SQL_TSI_DAY', -730, w.Id.MostRecentWeight.MostRecentWeightDate) THEN
Round(((w.Id.MostRecentWeight.MostRecentWeight - w.weight) * 100 / w.weight), 1)
ELSE
null
END AS PctChange,

CASE WHEN w.date >= timestampadd('SQL_TSI_DAY', -730, w.Id.MostRecentWeight.MostRecentWeightDate) THEN
Abs(Round(((w.Id.MostRecentWeight.MostRecentWeight - w.weight) * 100 / w.weight), 1))
else
null
END AS AbsPctChange,
w.qcstate
FROM study.weight w
WHERE w.Id.curlocation.area NOT IN ('Shelters', 'Corral', 'Hospital')-- Exclude animals from these locations
AND NOT (-- Exclude females under 5yrs, males under 7yrs
(w.Id.demographics.gender.code = 'f' AND w.Id.age.ageInYears < 5)
OR (w.Id.demographics.gender.code = 'm' AND w.Id.age.ageInYears < 7)
)
AND w.qcstate.publicdata = true
AND NOT EXISTS (
-- -- Find animals whose latest 'Weight MMA BEGIN' has no later 'Weight MMA RELEASE'
SELECT 1
FROM study.WeightManagementMMAData b
WHERE b.Id = w.Id
AND b.code = 'P-YY961'
AND b.date = (
SELECT MAX(b2.date)
FROM study.WeightManagementMMAData b2
WHERE b2.Id = w.Id
AND b2.code = 'P-YY961'
)
AND NOT EXISTS (
SELECT 1
FROM study.WeightManagementMMAData r
WHERE r.Id = w.Id
AND r.code = 'P-YY960'
AND r.date > b.date
)
)

Loading