diff --git a/onprc_ehr/resources/queries/study/demographicsParents.query.xml b/onprc_ehr/resources/queries/study/demographicsParents.query.xml
index e3ef57f38..43f12bfe3 100644
--- a/onprc_ehr/resources/queries/study/demographicsParents.query.xml
+++ b/onprc_ehr/resources/queries/study/demographicsParents.query.xml
@@ -8,7 +8,7 @@
true
- Dam
+ Genetic Dam
study
animal
@@ -31,8 +31,16 @@
id
+
+ Surrogate Dam
+
+ study
+ animal
+ id
+
+
- Genetic Dam
+ Genetic Dam Type
study
animal
@@ -42,6 +50,14 @@
Number of Parents Known
+
+
+ Birth Record Dam
+
+
+
+ Surrogate Dam
+
diff --git a/onprc_ehr/resources/queries/study/demographicsParents.sql b/onprc_ehr/resources/queries/study/demographicsParents.sql
index 91f7029d2..2979896f4 100644
--- a/onprc_ehr/resources/queries/study/demographicsParents.sql
+++ b/onprc_ehr/resources/queries/study/demographicsParents.sql
@@ -16,12 +16,11 @@
SELECT
d.id,
- coalesce(p2.parent, b.dam) as dam,
+ p2.parent as geneticdam,
CASE
WHEN p2.parent IS NOT NULL THEN p2.method
- WHEN b.dam IS NOT NULL THEN 'Observed'
- ELSE null
- END as damType,
+ ELSE null
+ END as geneticdamType,
coalesce(p1.parent, b.sire) as sire,
CASE
@@ -31,24 +30,28 @@ SELECT
END as sireType,
p3.parent as fosterMom,
p3.method as fosterType,
+ p4.parent as SurrogateDam,
+ p4.method as SurrogateType,
+ b.dam as birthdam,
(CASE WHEN p3.parent IS NOT NULL THEN 1 ELSE 0 END +
+ CASE WHEN p4.parent IS NOT NULL THEN 1 ELSE 0 END +
CASE WHEN coalesce(p2.parent, b.dam) IS NOT NULL THEN 1 ELSE 0 END +
CASE WHEN coalesce(p1.parent, b.sire) IS NOT NULL THEN 1 ELSE 0 END) as numParents,
- greatest(d.modified, p1.modified, p2.modified, p3.modified, b.modified) as modified
+ greatest(d.modified, p1.modified, p2.modified, p3.modified, p4.modified, b.modified) as modified
FROM study.demographics d
LEFT JOIN (
select p1.id, min(p1.method) as method, max(p1.parent) as parent, max(p1.modified) as modified
FROM study.parentage p1
- WHERE (p1.method = 'Genetic' OR p1.method = 'Provisional Genetic') AND p1.relationship = 'Sire' AND p1.enddate IS NULL
+ WHERE p1.method in ('Genetic','Provisional Genetic','Observed') AND p1.relationship = 'Sire' AND p1.enddate IS NULL
GROUP BY p1.Id
) p1 ON (d.Id = p1.id)
LEFT JOIN (
select p2.id, min(p2.method) as method, max(p2.parent) as parent, max(p2.modified) as modified
FROM study.parentage p2
- WHERE (p2.method = 'Genetic' OR p2.method = 'Provisional Genetic') AND p2.relationship = 'Dam' AND p2.enddate IS NULL
+ WHERE p2.method in ('Genetic','Provisional Genetic','Observed') AND p2.relationship = 'Dam' AND p2.enddate IS NULL
GROUP BY p2.Id
) p2 ON (d.Id = p2.id)
@@ -58,5 +61,12 @@ LEFT JOIN (
WHERE p3.relationship = 'Foster Dam' AND p3.enddate IS NULL
GROUP BY p3.Id
) p3 ON (d.Id = p3.id)
+LEFT JOIN (
+ select p4.id, min(p4.method) as method, max(p4.parent) as parent, max(p4.modified) as modified
+ FROM study.parentage p4
+ WHERE p4.relationship = 'Surrogate Dam' AND p4.enddate IS NULL
+ GROUP BY p4.Id
+) p4 ON (d.Id = p4.id)
+
LEFT JOIN study.birth b ON (b.id = d.id)