Skip to content

Commit 67c66bf

Browse files
committed
More query improvements
1 parent 1590125 commit 67c66bf

File tree

5 files changed

+44
-4
lines changed

5 files changed

+44
-4
lines changed

SivStudies/resources/queries/study/demographicsChallengeAndArt.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ FROM (
1111
min(floor(age(t.DataSets.Demographics.birth, CASE WHEN t.category = 'SIV Infection' THEN t.date ELSE NULL END))) AS ageAtInfection,
1212

1313
group_concat(DISTINCT CASE
14-
WHEN t.category = 'ART' THEN (t.treatment || ' (' || t.timePostSivChallenge.timePostInfection || ')')
14+
WHEN t.category = 'ART' THEN (t.treatment || ' (' || COALESCE(t.timePostSivChallenge.timePostInfection, 'Unk DPI') || ')')
1515
ELSE NULL
1616
END, char(10)) as allART,
1717
min(CASE

SivStudies/resources/queries/study/demographicsInterventions.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
SELECT
22
t.Id,
3-
group_concat(DISTINCT (t.treatment || ' (' || t.timePostSivChallenge.timePostInfection || ')'), char(10)) as allInterventions,
3+
group_concat(DISTINCT (t.treatment || ' (' || COALESCE(t.timePostSivChallenge.timePostInfection, 'Unk DPI') || ')'), char(10)) as allInterventions,
44
min(t.date) as firstInterventionDate,
55
min(t.timePostSivChallenge.daysPostInfection) as firstInterventionDPI,
66
min(t.timePostSivChallenge.weeksPostInfection) as firstInterventionWPI,
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<query xmlns="http://labkey.org/data/xml/query">
2+
<metadata>
3+
<tables xmlns="http://labkey.org/data/xml">
4+
<table tableName="missingAnchorDates" tableDbType="TABLE" useColumnOrder="true">
5+
<tableTitle>Missing Anchor Dates</tableTitle>
6+
</table>
7+
</tables>
8+
</metadata>
9+
</query>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
SELECT
2+
dc.Id as SubjectId,
3+
dc.infectionDate as date,
4+
'SIV Infection' as eventLabel
5+
6+
FROM study.demographicsChallengeAndArt dc
7+
LEFT JOIN studies.subjectAnchorDates ad ON (dc.Id = ad.SubjectId AND dc.infectionDate = ad.date AND ad.eventLabel = 'SIV Infection')
8+
WHERE ad.rowid IS NULL AND dc.infectionDate IS NOT NULL
9+
10+
UNION ALL
11+
12+
SELECT
13+
dc.Id as SubjectId,
14+
dc.artInitiationDate as date,
15+
'ART Initiation' as eventLabel
16+
17+
FROM study.demographicsChallengeAndArt dc
18+
LEFT JOIN studies.subjectAnchorDates ad ON (dc.Id = ad.SubjectId AND dc.artInitiationDate = ad.date AND ad.eventLabel = 'ART Initiation')
19+
WHERE ad.rowid IS NULL AND dc.artInitiationDate IS NOT NULL
20+
21+
UNION ALL
22+
23+
SELECT
24+
dc.Id as SubjectId,
25+
dc.artReleaseDate as date,
26+
'ART Release' as eventLabel
27+
28+
FROM study.demographicsChallengeAndArt dc
29+
LEFT JOIN studies.subjectAnchorDates ad ON (dc.Id = ad.SubjectId AND dc.artReleaseDate = ad.date AND ad.eventLabel = 'ART Release')
30+
WHERE ad.rowid IS NULL AND dc.artReleaseDate IS NOT NULL

SivStudies/src/org/labkey/sivstudies/query/SivStudiesCustomizer.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ public TableInfo getLookupTableInfo()
382382
"JOIN studies.subjectAnchorDates ad ON (ad.subjectId = c." + idCol.getFieldKey().toSQLString() + ")\n" +
383383
"WHERE ad.eventLabel = 'SIV Infection'\n" +
384384
"GROUP BY c.date, c." + pkCol.getFieldKey().toString() + "\n" +
385-
"HAVING count(*) = 1) t"
385+
"HAVING count(DISTINCT c.date) = 1) t"
386386
);
387387
qd.setIsTemporary(true);
388388

@@ -472,9 +472,10 @@ public TableInfo getLookupTableInfo()
472472
"GROUP_CONCAT(DISTINCT tr.treatment) AS artTreatment,\n" +
473473
"c." + pkCol.getFieldKey().toString() + "\n" +
474474
"FROM \"" + schemaName + "\".\"" + queryName + "\" c " +
475+
// TODO: consider whether this should include all dates
475476
"JOIN study.treatments tr ON (tr.category = 'ART' AND CAST(tr.date AS DATE) <= CAST(c." + dateCol.getFieldKey().toString() + " AS DATE) AND tr.Id = c." + idCol.getFieldKey().toSQLString() + ")\n" +
476477
"GROUP BY c." + dateCol.getFieldKey().toString() + ", c." + pkCol.getFieldKey().toString() + "\n" +
477-
"HAVING COUNT(*) = 1"
478+
"HAVING COUNT(DISTINCT tr.date) = 1"
478479
);
479480
qd.setIsTemporary(true);
480481

0 commit comments

Comments
 (0)