1+ /* *
2+ * Demographics
3+ */
4+
5+ BEGIN
6+
7+ DECLARE @yes BIT = 1
8+ DECLARE @no BIT = 0
9+
10+ DECLARE @sqlset_person INT = (SELECT TOP 1 Id FROM LeafDB .app .ConceptSqlSet WHERE SqlSetFrom = ' dbo.person' )
11+ DECLARE @sqlset_visit_occurrence INT = (SELECT TOP 1 Id FROM LeafDB .app .ConceptSqlSet WHERE SqlSetFrom = ' dbo.visit_occurrence' )
12+ DECLARE @sqlset_condition_occurrence INT = (SELECT TOP 1 Id FROM LeafDB .app .ConceptSqlSet WHERE SqlSetFrom = ' dbo.condition_occurrence' )
13+ DECLARE @sqlset_v_death INT = (SELECT TOP 1 Id FROM LeafDB .app .ConceptSqlSet WHERE SqlSetFrom = ' dbo.v_death' )
14+ DECLARE @sqlset_device_exposure INT = (SELECT TOP 1 Id FROM LeafDB .app .ConceptSqlSet WHERE SqlSetFrom = ' dbo.device_exposure' )
15+ DECLARE @sqlset_drug_exposure INT = (SELECT TOP 1 Id FROM LeafDB .app .ConceptSqlSet WHERE SqlSetFrom = ' dbo.drug_exposure' )
16+ DECLARE @sqlset_measurement INT = (SELECT TOP 1 Id FROM LeafDB .app .ConceptSqlSet WHERE SqlSetFrom = ' dbo.measurement' )
17+ DECLARE @sqlset_observation INT = (SELECT TOP 1 Id FROM LeafDB .app .ConceptSqlSet WHERE SqlSetFrom = ' dbo.observation' )
18+ DECLARE @sqlset_procedure_occurrence INT = (SELECT TOP 1 Id FROM LeafDB .app .ConceptSqlSet WHERE SqlSetFrom = ' dbo.procedure_occurrence' )
19+
20+ DECLARE @demog_root NVARCHAR (50 ) = ' demographics'
21+ DECLARE @demog_gender NVARCHAR (50 ) = ' demographics:gender'
22+ DECLARE @demog_ethnic NVARCHAR (50 ) = ' demographics:ethnicity'
23+ DECLARE @demog_race NVARCHAR (50 ) = ' demographics:race'
24+ DECLARE @demog_age NVARCHAR (50 ) = ' demographics:age'
25+ DECLARE @demog_vital NVARCHAR (50 ) = ' demographics:vital'
26+
27+ ; WITH gender AS
28+ (
29+ SELECT C .concept_name , C .concept_id , cnt = COUNT (DISTINCT person_id), concept_id_string = CONVERT (NVARCHAR (50 ), C .concept_id )
30+ FROM dbo .person AS X INNER JOIN dbo .concept AS C
31+ ON X .gender_concept_id = C .concept_id
32+ WHERE X .gender_concept_id != 0
33+ GROUP BY C .concept_name , C .concept_id
34+ ), ethnicity AS
35+ (
36+ SELECT C .concept_name , C .concept_id , cnt = COUNT (DISTINCT person_id), concept_id_string = CONVERT (NVARCHAR (50 ), C .concept_id )
37+ FROM dbo .person AS X INNER JOIN dbo .concept AS C
38+ ON X .ethnicity_concept_id = C .concept_id
39+ WHERE X .ethnicity_concept_id != 0
40+ GROUP BY C .concept_name , C .concept_id
41+ ), race AS
42+ (
43+ SELECT C .concept_name , C .concept_id , cnt = COUNT (DISTINCT person_id), concept_id_string = CONVERT (NVARCHAR (50 ), C .concept_id )
44+ FROM dbo .person AS X INNER JOIN dbo .concept AS C
45+ ON X .race_concept_id = C .concept_id
46+ WHERE X .race_concept_id != 0
47+ GROUP BY C .concept_name , C .concept_id
48+ )
49+
50+ /* INSERT */
51+ INSERT INTO LeafDB .app .Concept (ExternalId, ExternalParentId, [IsNumeric], IsParent, IsRoot, SqlSetId, SqlSetWhere,
52+ SqlFieldNumeric, UiDisplayName, UiDisplayText, UiDisplayUnits, UiNumericDefaultText, UiDisplayPatientCount)
53+
54+ /* Root */
55+ SELECT ExternalId = @demog_root
56+ , ExternalParentId = NULL
57+ , [IsNumeric] = @no
58+ , IsParent = @yes
59+ , IsRoot = @yes
60+ , SqlSetId = @sqlset_person
61+ , SqlSetWhere = NULL
62+ , SqlFieldNumeric = NULL
63+ , UiDisplayName = ' Demographics'
64+ , UiDisplayText = ' Have demographics'
65+ , UiDisplayUnits = NULL
66+ , UiNumericDefaultText = NULL
67+ , UiDisplayPatientCount = (SELECT COUNT (* ) FROM dbo .person )
68+ UNION ALL
69+
70+ /* Gender */
71+ SELECT ExternalId = @demog_gender
72+ , ExternalParentId = @demog_root
73+ , [IsNumeric] = @no
74+ , IsParent = @yes
75+ , IsRoot = @no
76+ , SqlSetId = @sqlset_person
77+ , SqlSetWhere = NULL
78+ , SqlFieldNumeric = NULL
79+ , UiDisplayName = ' Gender'
80+ , UiDisplayText = ' Have gender data'
81+ , UiDisplayUnits = NULL
82+ , UiNumericDefaultText = NULL
83+ , UiDisplayPatientCount = (SELECT SUM (cnt) FROM gender)
84+ UNION ALL
85+ SELECT ExternalId = @demog_gender + ' :' + X .concept_id_string
86+ , ExternalParentId = @demog_gender
87+ , [IsNumeric] = @no
88+ , IsParent = @no
89+ , IsRoot = @no
90+ , SqlSetId = @sqlset_person
91+ , SqlSetWhere = ' /* ' + X .concept_name + ' */ @.gender_concept_id = ' + X .concept_id_string
92+ , SqlFieldNumeric = NULL
93+ , UiDisplayName = X .concept_name
94+ , UiDisplayText = ' Identify as ' + X .concept_name
95+ , UiDisplayUnits = NULL
96+ , UiNumericDefaultText = NULL
97+ , UiDisplayPatientCount = X .cnt
98+ FROM gender AS X
99+ UNION ALL
100+
101+ /* Ethnicity */
102+ SELECT ExternalId = @demog_ethnic
103+ , ExternalParentId = @demog_root
104+ , [IsNumeric] = @no
105+ , IsParent = @yes
106+ , IsRoot = @no
107+ , SqlSetId = @sqlset_person
108+ , SqlSetWhere = NULL
109+ , SqlFieldNumeric = NULL
110+ , UiDisplayName = ' Ethncity'
111+ , UiDisplayText = ' Have ethnicity data'
112+ , UiDisplayUnits = NULL
113+ , UiNumericDefaultText = NULL
114+ , UiDisplayPatientCount = (SELECT SUM (cnt) FROM ethnicity)
115+ UNION ALL
116+ SELECT ExternalId = @demog_ethnic + ' :' + X .concept_id_string
117+ , ExternalParentId = @demog_ethnic
118+ , [IsNumeric] = @no
119+ , IsParent = @no
120+ , IsRoot = @no
121+ , SqlSetId = @sqlset_person
122+ , SqlSetWhere = ' /* ' + X .concept_name + ' */ @.ethnicity_concept_id = ' + X .concept_id_string
123+ , SqlFieldNumeric = NULL
124+ , UiDisplayName = X .concept_name
125+ , UiDisplayText = ' Identify as ' + X .concept_name
126+ , UiDisplayUnits = NULL
127+ , UiNumericDefaultText = NULL
128+ , UiDisplayPatientCount = X .cnt
129+ FROM ethnicity AS X
130+ UNION ALL
131+
132+ /* Race */
133+ SELECT ExternalId = @demog_race
134+ , ExternalParentId = @demog_root
135+ , [IsNumeric] = @no
136+ , IsParent = @yes
137+ , IsRoot = @no
138+ , SqlSetId = @sqlset_person
139+ , SqlSetWhere = NULL
140+ , SqlFieldNumeric = NULL
141+ , UiDisplayName = ' Race'
142+ , UiDisplayText = ' Have race data'
143+ , UiDisplayUnits = NULL
144+ , UiNumericDefaultText = NULL
145+ , UiDisplayPatientCount = (SELECT SUM (cnt) FROM race)
146+ UNION ALL
147+ SELECT ExternalId = @demog_race + ' :' + X .concept_id_string
148+ , ExternalParentId = @demog_race
149+ , [IsNumeric] = @no
150+ , IsParent = @no
151+ , IsRoot = @no
152+ , SqlSetId = @sqlset_person
153+ , SqlSetWhere = ' /* ' + X .concept_name + ' */ @.race_concept_id = ' + X .concept_id_string
154+ , SqlFieldNumeric = NULL
155+ , UiDisplayName = X .concept_name
156+ , UiDisplayText = ' Identify as ' + X .concept_name
157+ , UiDisplayUnits = NULL
158+ , UiNumericDefaultText = NULL
159+ , UiDisplayPatientCount = X .cnt
160+ FROM race AS X
161+ UNION ALL
162+
163+ /* Age */
164+ SELECT ExternalId = @demog_age
165+ , ExternalParentId = @demog_root
166+ , [IsNumeric] = @yes
167+ , IsParent = @no
168+ , IsRoot = @no
169+ , SqlSetId = @sqlset_person
170+ , SqlSetWhere = NULL
171+ , SqlFieldNumeric = ' /* Current Age */ (DATEDIFF(DAY, @.birth_datetime, GETDATE()) / 365.25)'
172+ , UiDisplayName = ' Age'
173+ , UiDisplayText = ' Are'
174+ , UiDisplayUnits = ' years old'
175+ , UiNumericDefaultText = ' any current age'
176+ , UiDisplayPatientCount = (SELECT COUNT (* ) FROM dbo .person )
177+ UNION ALL
178+
179+ /* Vital status */
180+ SELECT ExternalId = @demog_vital
181+ , ExternalParentId = @demog_root
182+ , [IsNumeric] = @no
183+ , IsParent = @yes
184+ , IsRoot = @no
185+ , SqlSetId = @sqlset_person
186+ , SqlSetWhere = NULL
187+ , SqlFieldNumeric = NULL
188+ , UiDisplayName = ' Vital Status'
189+ , UiDisplayText = ' Are living or deceased'
190+ , UiDisplayUnits = NULL
191+ , UiNumericDefaultText = NULL
192+ , UiDisplayPatientCount = (SELECT COUNT (* ) FROM dbo .person )
193+ UNION ALL
194+
195+ /* Living */
196+ SELECT ExternalId = @demog_vital + ' :living'
197+ , ExternalParentId = @demog_vital
198+ , [IsNumeric] = @no
199+ , IsParent = @no
200+ , IsRoot = @no
201+ , SqlSetId = @sqlset_person
202+ , SqlSetWhere = ' /* Not deceased */ NOT EXISTS (SELECT 1 FROM dbo.death AS @D WHERE @.person_id = @D.person_id)'
203+ , SqlFieldNumeric = NULL
204+ , UiDisplayName = ' Living'
205+ , UiDisplayText = ' Are living or not known to be deceased'
206+ , UiDisplayUnits = NULL
207+ , UiNumericDefaultText = NULL
208+ , UiDisplayPatientCount = (SELECT COUNT (* ) FROM dbo .person AS P WHERE NOT EXISTS (SELECT 1 FROM dbo .death AS D WHERE P .person_id = D .person_id ))
209+ UNION ALL
210+
211+ /* Deceased */
212+ SELECT ExternalId = @demog_vital + ' :deceased'
213+ , ExternalParentId = @demog_vital
214+ , [IsNumeric] = @no
215+ , IsParent = @no
216+ , IsRoot = @no
217+ , SqlSetId = @sqlset_v_death
218+ , SqlSetWhere = NULL
219+ , SqlFieldNumeric = NULL
220+ , UiDisplayName = ' Deceased'
221+ , UiDisplayText = ' Are known to be deceased'
222+ , UiDisplayUnits = NULL
223+ , UiNumericDefaultText = NULL
224+ , UiDisplayPatientCount = (SELECT COUNT (* ) FROM dbo .person AS P WHERE EXISTS (SELECT 1 FROM dbo .death AS D WHERE P .person_id = D .person_id ))
225+
226+ /* *
227+ * Set ParentId based on ExternalIds
228+ */
229+ UPDATE LeafDB .app .Concept
230+ SET ParentId = P .Id
231+ FROM LeafDB .app .Concept AS C
232+ INNER JOIN (SELECT P .Id , P .ParentId , P .ExternalId
233+ FROM LeafDB .app .Concept AS P) AS P
234+ ON C .ExternalParentID = P .ExternalID
235+ WHERE C .ParentId IS NULL
236+
237+ /* *
238+ * Set RootIds
239+ */
240+ ; WITH roots AS
241+ (
242+ SELECT RootId = C .Id
243+ , RootUiDisplayName = C .UiDisplayName
244+ , C .IsRoot
245+ , C .Id
246+ , C .ParentId
247+ , C .UiDisplayName
248+ FROM LeafDB .app .Concept AS C
249+ WHERE C .IsRoot = 1
250+
251+ UNION ALL
252+
253+ SELECT roots .RootId
254+ , roots .RootUiDisplayName
255+ , C2 .IsRoot
256+ , C2 .Id
257+ , C2 .ParentId
258+ , C2 .UiDisplayName
259+ FROM roots
260+ INNER JOIN LeafDB .app .Concept AS C2
261+ ON C2 .ParentId = roots .Id
262+ )
263+
264+ UPDATE LeafDB .app .Concept
265+ SET RootId = roots .RootId
266+ FROM LeafDB .app .Concept AS C
267+ INNER JOIN roots
268+ ON C .Id = roots .Id
269+ WHERE C .RootId IS NULL
270+
271+ END
0 commit comments