Skip to content
Open
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', 'Catch Area')-- 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>false</isHidden>
<fk>
<fkDbSchema>study</fkDbSchema>
<fkTable>animal</fkTable>
<fkColumnName>id</fkColumnName>
</fk>
</column>
<column columnName="date">
<isHidden>false</isHidden>
<columnTitle>Prev Weight Date</columnTitle>
</column>
<column columnName="weight">
<columnTitle>Prev 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>
85 changes: 85 additions & 0 deletions onprc_ehr/resources/queries/study/weightRelChange_NotInMMA.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
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 DISTINCT
l.lsid,
l.Id,

l.date AS LatestWeightDate,
l.weight AS LatestWeight,

p.date, -- AS PrevWeightDate
p.weight, -- AS PrevWeight

timestampdiff('SQL_TSI_DAY', p.date, l.date) AS IntervalInDays,
age_in_months(p.date, l.date) AS IntervalInMonths,

CASE
WHEN p.weight IS NOT NULL AND p.weight > 0 THEN
ROUND(((l.weight - p.weight) * 100 / p.weight), 1)
ELSE NULL
END AS PctChange,

CASE
WHEN p.weight IS NOT NULL AND p.weight > 0 THEN
ABS(ROUND(((l.weight - p.weight) * 100 / p.weight), 1))
ELSE NULL
END AS AbsPctChange,

l.qcstate AS LatestQcState,
p.qcstate AS PrevQcState

FROM
(SELECT Id, MAX(date) AS LatestDate
FROM study.weight
GROUP BY Id) lw

JOIN study.weight l
ON l.Id = lw.Id
AND l.date = lw.LatestDate

LEFT JOIN study.weight p
ON p.Id = lw.Id
AND p.date = (
SELECT MAX(w2.date)
FROM study.weight w2
WHERE w2.Id = lw.Id
AND w2.date <= timestampadd('SQL_TSI_DAY', -30, lw.LatestDate)
AND w2.date >= timestampadd('SQL_TSI_DAY', -100, lw.LatestDate)
)

WHERE l.Id.curlocation.area NOT IN ('Shelters', 'Corral', 'Hospital', 'Catch Area')-- Exclude animals from these locations
AND NOT (-- Exclude females under 5yrs, males under 7yrs
(l.Id.demographics.gender.code = 'f' AND l.Id.age.ageInYears < 5)
OR (l.Id.demographics.gender.code = 'm' AND l.Id.age.ageInYears < 7)
)
AND l.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 = l.Id
AND b.code = 'P-YY961'
AND b.date = (
SELECT MAX(b2.date)
FROM study.WeightManagementMMAData b2
WHERE b2.Id = l.Id
AND b2.code = 'P-YY961'
)
AND NOT EXISTS (
SELECT 1
FROM study.WeightManagementMMAData r
WHERE r.Id = l.Id
AND r.code = 'P-YY960'
AND r.date > b.date
)
)

Loading