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,37 @@
<query xmlns="http://labkey.org/data/xml/query">
<metadata>
<tables xmlns="http://labkey.org/data/xml">
<table tableName="demographicsCurrentRoommates" tableDbType="NOT_IN_DB">
<columns>
<column columnName="Id">
<isKeyField>true</isKeyField>
<isHidden>true</isHidden>
</column>
<column columnName="NumRoommates">
<columnTitle># Cagemates</columnTitle>
<description>The total number of animals in this cage, excluding the current animal</description>
<url>/query/executeQuery.view?schemaName=study&amp;
query.queryName=housing&amp;
query.room~eq=${Id/curLocation/room/room}&amp;
query.cage~eq=${Id/curLocation/cage}&amp;
query.enddate~isblank&amp;
query.sort=Id&amp;
</url>
</column>
<column columnName="AnimalsInCage">
<columnTitle>Total Animals In Cage</columnTitle>
<description>The total number of animals in this cage, including the current animal</description>
<url>/query/executeQuery.view?schemaName=study&amp;
query.queryName=housing&amp;
query.room~eq=${Id/curLocation/room/room}&amp;
query.cage~eq=${Id/curLocation/cage}&amp;
query.enddate~isblank&amp;
query.sort=Id&amp;
</url>
</column>
</columns>
<titleColumn>NumRoommates</titleColumn>
</table>
</tables>
</metadata>
</query>
31 changes: 31 additions & 0 deletions onprc_ehr/resources/queries/study/demographicsCurrentRoommates.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2010-2019 LabKey Corporation
*
* Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0
*/
SELECT
d.id,
count(DISTINCT h.RoommateId) as NumRoommates,
(count(DISTINCT h.RoommateId)+1) as AnimalsInCage,
group_concat(DISTINCT h.RoommateId,', ') as cagemates

FROM study.demographics d
LEFT JOIN (
SELECT
h1.id,
h2.id as RoommateId
FROM study.Housing h1
LEFT OUTER JOIN study.Housing h2 ON (
h1.enddate IS NULL AND -- only look at current housing assignments (no end date)
h2.enddate IS NULL AND -- only look at current housing assignments (no end date)
h1.id != h2.id AND -- don't include self as roommate
h1.room = h2.room AND -- Make sure room matches
(h1.cage = h2.cage OR (h1.cage is null and h2.cage is null)) -- make sure cage matches
)
WHERE h1.qcstate.publicdata = true AND h2.qcstate.publicdata = true
) h
ON (h.id = d.id)

WHERE d.calculated_status='Alive'

GROUP BY d.id