@@ -17,8 +17,9 @@ CREATE TABLE @results_schema.achilles_result_concept_count
1717/* *********************************************/
1818/* **** Populate record/person count table *****/
1919/* *********************************************/
20+ IF OBJECT_ID(' tempdb.#tmp_counts' , ' U' ) IS NOT NULL DROP TABLE # tmp_counts;
2021WITH counts AS (
21- SELECT stratum_1 concept_id, MAX (count_value) agg_count_value
22+ SELECT stratum_1 AS concept_id, MAX (count_value) AS agg_count_value
2223 FROM @results_schema .achilles_results
2324 WHERE analysis_id IN (2 , 4 , 5 , 201 , 225 , 301 , 325 , 401 , 425 , 501 , 505 , 525 , 601 , 625 , 701 , 725 , 801 , 825 ,
2425 826 , 827 , 901 , 1001 , 1201 , 1203 , 1425 , 1801 , 1825 , 1826 , 1827 , 2101 , 2125 , 2301 )
@@ -58,7 +59,7 @@ WITH counts AS (
5859 */
5960 GROUP BY stratum_1
6061 UNION ALL
61- SELECT stratum_2 concept_id, SUM (count_value) AS agg_count_value
62+ SELECT stratum_2 AS concept_id, SUM (count_value) AS agg_count_value
6263 FROM @results_schema .achilles_results
6364 WHERE analysis_id IN (405 , 605 , 705 , 805 , 807 , 1805 , 1807 , 2105 )
6465 /* analyses:
@@ -73,8 +74,15 @@ WITH counts AS (
7374 but this subquery only gets the type or unit concept_ids, i.e., stratum_2
7475 */
7576 GROUP BY stratum_2
76- ), counts_person AS (
77- SELECT stratum_1 as concept_id, MAX (count_value) agg_count_value
77+ )
78+ SELECT concept_id,
79+ agg_count_value
80+ INTO # tmp_counts
81+ FROM counts;
82+
83+ IF OBJECT_ID(' tempdb.#tmp_counts_person' , ' U' ) IS NOT NULL DROP TABLE # tmp_counts_person;
84+ WITH counts_person AS (
85+ SELECT stratum_1 AS concept_id, MAX (count_value) AS agg_count_value
7886 FROM @results_schema .achilles_results
7987 WHERE analysis_id IN (200 , 240 , 400 , 440 , 540 , 600 , 640 , 700 , 740 , 800 , 840 , 900 , 1000 , 1300 , 1340 , 1800 , 1840 , 2100 , 2140 , 2200 )
8088 /* analyses:
@@ -100,28 +108,44 @@ WITH counts AS (
100108 Number of persons with at least one note by note_type_concept_id
101109 */
102110 GROUP BY stratum_1
103- ), concepts AS (
111+ )
112+ SELECT concept_id,
113+ agg_count_value
114+ INTO # tmp_counts_person
115+ FROM counts_person;
116+
117+ IF OBJECT_ID(' tempdb.#tmp_concepts' , ' U' ) IS NOT NULL DROP TABLE # tmp_concepts;
118+ WITH concepts AS (
104119 select concept_id as ancestor_id, coalesce(cast(ca .descendant_concept_id as varchar (50 )), concept_id) as descendant_id
105120 from (
106- select concept_id from counts
121+ select concept_id from # tmp_counts
107122 UNION
108123 -- include any ancestor concept that has a descendant in counts
109124 select distinct cast(ancestor_concept_id as varchar (50 )) concept_id
110- from counts c
125+ from # tmp_counts c
111126 join @vocab_schema .concept_ancestor ca on cast(ca .descendant_concept_id as varchar (50 )) = c .concept_id
112127 ) c
113128 left join @vocab_schema .concept_ancestor ca on c .concept_id = cast(ca .ancestor_concept_id as varchar (50 ))
114129)
130+ SELECT ancestor_id,
131+ descendant_id
132+ INTO # tmp_concepts
133+ FROM concepts;
134+
115135INSERT INTO @results_schema .achilles_result_concept_count (concept_id, record_count, descendant_record_count, person_count, descendant_person_count)
116- SELECT
117- cast(concepts .ancestor_id as int ) concept_id,
118- coalesce(max (c1 .agg_count_value ), 0 ) record_count,
119- coalesce(sum (c2 .agg_count_value ), 0 ) descendant_record_count,
120- coalesce(max (c3 .agg_count_value ), 0 ) person_count,
121- coalesce(sum (c4 .agg_count_value ), 0 ) descendant_person_count
122- FROM concepts
123- LEFT JOIN counts c1 ON concepts .ancestor_id = c1 .concept_id
124- LEFT JOIN counts c2 ON concepts .descendant_id = c2 .concept_id
125- LEFT JOIN counts_person c3 ON concepts .ancestor_id = c3 .concept_id
126- LEFT JOIN counts_person c4 ON concepts .descendant_id = c4 .concept_id
136+ SELECT DISTINCT
137+ cast(concepts .ancestor_id as int ) AS concept_id,
138+ coalesce(max (c1 .agg_count_value ), 0 ) AS record_count,
139+ coalesce(sum (c2 .agg_count_value ), 0 ) AS descendant_record_count,
140+ coalesce(max (c3 .agg_count_value ), 0 ) AS person_count,
141+ coalesce(sum (c4 .agg_count_value ), 0 ) AS descendant_person_count
142+ FROM # tmp_concepts concepts
143+ LEFT JOIN # tmp_counts c1 ON concepts.ancestor_id = c1.concept_id
144+ LEFT JOIN # tmp_counts c2 ON concepts.descendant_id = c2.concept_id
145+ LEFT JOIN # tmp_counts_person c3 ON concepts.ancestor_id = c3.concept_id
146+ LEFT JOIN # tmp_counts_person c4 ON concepts.descendant_id = c4.concept_id
127147GROUP BY concepts .ancestor_id ;
148+
149+ IF OBJECT_ID(' tempdb.#tmp_counts' , ' U' ) IS NOT NULL DROP TABLE # tmp_counts;
150+ IF OBJECT_ID(' tempdb.#tmp_counts_person' , ' U' ) IS NOT NULL DROP TABLE # tmp_counts_person;
151+ IF OBJECT_ID(' tempdb.#tmp_concepts' , ' U' ) IS NOT NULL DROP TABLE # tmp_concepts;
0 commit comments