From d4d1e33dc94f60dd588fb099f277653e4858a396 Mon Sep 17 00:00:00 2001 From: Marty Pradere Date: Sun, 23 Aug 2026 16:47:11 -0600 Subject: [PATCH 1/3] Filter cagemates to current public housing and guard null locations The joined housing row was never filtered for end date or QC state, so closed and In Progress rows counted as current cagemates. Housing rows whose location never resolved are now excluded rather than grouped together. --- .../resources/queries/study/demographicsCagemates.sql | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/nirc_ehr/resources/queries/study/demographicsCagemates.sql b/nirc_ehr/resources/queries/study/demographicsCagemates.sql index a309200f..3581c8c2 100644 --- a/nirc_ehr/resources/queries/study/demographicsCagemates.sql +++ b/nirc_ehr/resources/queries/study/demographicsCagemates.sql @@ -22,10 +22,15 @@ SELECT FROM study.housing h JOIN study.housing h2 -ON (h2.Id.demographics.calculated_status = 'Alive' - AND (h.cage = h2.cage)) +ON (h.cage = h2.cage + AND h2.Id.demographics.calculated_status = 'Alive' + AND h2.enddateTimeCoalesced >= now() + AND h2.qcstate.publicdata = true) -WHERE h.enddateTimeCoalesced >= now() +-- cage holds the ehr_lookups.cage location key, so a null means this row's location never resolved; such a row gets no cagemates rather than sharing one group with every other unresolved row +WHERE h.cage IS NOT NULL +AND h.enddateTimeCoalesced >= now() +AND h.qcstate.publicdata = true GROUP BY h.id, h.room, h.cage ) t ON (t.id = d.id) From 0916041a41e97258df6ebb672208b26adcc1101a Mon Sep 17 00:00:00 2001 From: Marty Pradere Date: Mon, 24 Aug 2026 05:47:45 -0600 Subject: [PATCH 2/3] Filter cagemates on demographics QC state --- nirc_ehr/resources/queries/study/demographicsCagemates.sql | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/nirc_ehr/resources/queries/study/demographicsCagemates.sql b/nirc_ehr/resources/queries/study/demographicsCagemates.sql index 3581c8c2..5c8bc7cd 100644 --- a/nirc_ehr/resources/queries/study/demographicsCagemates.sql +++ b/nirc_ehr/resources/queries/study/demographicsCagemates.sql @@ -35,4 +35,5 @@ GROUP BY h.id, h.room, h.cage ) t ON (t.id = d.id) -WHERE d.calculated_status = 'Alive' \ No newline at end of file +WHERE d.calculated_status = 'Alive' +AND d.qcstate.publicdata = true \ No newline at end of file From cdb203c318c0e7b7c0ad635eb852e1ea75caa208 Mon Sep 17 00:00:00 2001 From: Marty Pradere Date: Mon, 24 Aug 2026 14:46:58 -0600 Subject: [PATCH 3/3] Filter cagemates on both sides and drop redundant room grouping The demographics QC-state filter now applies to the joined animal as well, and room is no longer a grouping key since it is derived from the cage location. --- nirc_ehr/resources/queries/study/demographicsCagemates.sql | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/nirc_ehr/resources/queries/study/demographicsCagemates.sql b/nirc_ehr/resources/queries/study/demographicsCagemates.sql index 5c8bc7cd..efb814fd 100644 --- a/nirc_ehr/resources/queries/study/demographicsCagemates.sql +++ b/nirc_ehr/resources/queries/study/demographicsCagemates.sql @@ -24,6 +24,7 @@ FROM study.housing h JOIN study.housing h2 ON (h.cage = h2.cage AND h2.Id.demographics.calculated_status = 'Alive' + AND h2.Id.demographics.qcstate.publicdata = true AND h2.enddateTimeCoalesced >= now() AND h2.qcstate.publicdata = true) @@ -31,7 +32,7 @@ ON (h.cage = h2.cage WHERE h.cage IS NOT NULL AND h.enddateTimeCoalesced >= now() AND h.qcstate.publicdata = true -GROUP BY h.id, h.room, h.cage +GROUP BY h.id, h.cage ) t ON (t.id = d.id)