-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMOH-SQL-code.sql
295 lines (260 loc) · 14 KB
/
MOH-SQL-code.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
-- -----------------------------------
-- Here I provide queries that are complicated by the presence of long table and column names.
-- These queries were provided by Stats NZ as example queries for the New Zealand Integrated Data Infrastructure (not for the course practice databases).
-- Below the first two queries, I have provided simplified versions of the same query.
-- The only changes made for the simplified version are:
--
-- 1. assignment of aliases for table and column names
--
-- 2. changing indentation and adding/removing newlines
-- Note the queries are only 'simplified' if you know how the WITH clause works!
-- The subquery in the WITH clause is only assigning aliases.
-- The query below the WITH clause is doing all the 'work'.
-- I have only simplifed the first three. You can use the remaining queries to practice with (try to simplify them in the same manner).
-- -----------------------------------
-- -----------------------------------
--Clients seen by snz_uid
SELECT year(IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_start_date) AS StartYear ,
IDI_Clean_20181020.moh_clean.PRIMHD.snz_moh_uid
FROM IDI_Clean_20181020.moh_clean.PRIMHD
WHERE IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_type_code != 'T35'
GROUP BY year(IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_start_date),
IDI_Clean_20181020.moh_clean.PRIMHD.snz_moh_uid
-- simplified version
WITH Shortened AS (
SELECT M.moh_mhd_activity_start_date AS astart,
M.moh_clean.PRIMHD.snz_moh_uid AS muid,
M.moh_mhd_activity_type_code AS activity
FROM IDI_Clean_20181020.moh_clean.PRIMHD AS M
)
SELECT year(astart) AS StartYear,
FROM Shortened
WHERE activity != 'T35'
GROUP BY year(astart), muid
-- -----------------------------------
-- -----------------------------------
--Service users by snz_moh_uid
SELECT year(IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_start_date) AS StartYear,
IDI_Clean_20181020.moh_clean.PRIMHD.snz_moh_uid
FROM IDI_Clean_20181020.moh_clean.PRIMHD
WHERE IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_type_code != 'T35'
GROUP BY year(IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_start_date),
IDI_Clean_20181020.moh_clean.PRIMHD.snz_moh_uid
ORDER BY year(IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_start_date)
-- simplified version
WITH Shortened AS (
SELECT M.moh_mhd_activity_start_date AS astart,
M.snz_moh_uid AS muid,
M.moh_mhd_activity_type_code AS activity
FROM IDI_Clean_20181020.moh_clean.PRIMHD AS M
)
SELECT year(astart) AS StartYear, M.muid
FROM Shortened
WHERE M.activity != 'T35'
GROUP BY year(astart), M.muid
ORDER BY year(astart);
-- -----------------------------------
-- -----------------------------------
--Service users/AoD service users
SELECT year(IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_start_date) AS StartYear,
count (DISTINCT IDI_Clean_20181020.moh_clean.PRIMHD.snz_moh_uid) AS 'Service Users',
count (DISTINCT (case when (IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_team_type_code in ('03', '11') or
IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_type_code in ('T16', 'T17', 'T18', 'T19', 'T20','T48'))
then IDI_Clean_20181020.moh_clean.PRIMHD.snz_moh_uid else NULL end)) AS 'AoD Service Users'
FROM IDI_Clean_20181020.moh_clean.PRIMHD
WHERE IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_type_code != 'T35'
GROUP BY year(IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_start_date);
-- simplified version
WITH Shortened AS (
SELECT M.moh_mhd_activity_start_date AS astart,
M.snz_moh_uid AS muid,
M.moh_mhd_team_type_code AS team,
M.moh_mhd_activity_type_code AS activity
FROM IDI_Clean_20181020.moh_clean.PRIMHD M
)
SELECT year(astart) AS Year,
count(DISTINCT muid) AS 'Service Users',
count(DISTINCT(case when (team in ('03', '11') or activity in ('T16', 'T17', 'T18', 'T19', 'T20','T48'))
then muid else NULL end)) AS 'AoD Service Users'
FROM Shortened
WHERE type != 'T35'
GROUP BY year(astart);
-- -----------------------------------
-- -----------------------------------
--Service users by DHB/non-DHB
SELECT year(IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_start_date) AS StartYear ,
case when [IDI_Metadata].[clean_read_CLASSIFICATIONS].[moh_primhd_organisation_code].[ORGANISATION_TYPE] = 'District Health Board (DHB)' then 'DHB'
else 'non-DHB' end 'Organisation type',
count (distinct IDI_Clean_20181020.moh_clean.PRIMHD.snz_moh_uid) 'Count of service users'
FROM
IDI_Clean_20181020.moh_clean.PRIMHD
INNER JOIN IDI_Metadata.clean_read_CLASSIFICATIONS.moh_primhd_organisation_code on
IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_organisation_id_code = IDI_Metadata.clean_read_CLASSIFICATIONS.moh_primhd_organisation_code.ORGANISATION_ID
WHERE
IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_type_code != 'T35'
GROUP BY
year(IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_start_date),
case when [IDI_Metadata].[clean_read_CLASSIFICATIONS].[moh_primhd_organisation_code].[ORGANISATION_TYPE] = 'District Health Board (DHB)' then 'DHB'
else 'non-DHB' end
ORDER BY
year(IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_start_date)
-- -----------------------------------
-- -----------------------------------
--Face to face service users (clients seen)
--Clients seen/AoD clients seen
SELECT year(IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_start_date) AS StartYear,
count (distinct IDI_Clean_20181020.moh_clean.PRIMHD.snz_moh_uid) AS 'Clients Seen',
count ( distinct case when (IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_team_type_code in ('03', '11') or
IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_type_code in ('T16', 'T17', 'T18', 'T19', 'T20','T48')) then
(IDI_Clean_20181020.moh_clean.PRIMHD.snz_moh_uid) else NULL end) AS 'AoD_Clients_Seen'
FROM
IDI_Clean_20181020.moh_clean.PRIMHD
WHERE
IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_type_code not in ('T35','T32','T33','T37','T08')
AND IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_setting_code not in ('WR','PH','SM','OM')
GROUP BY
year(IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_start_date);
-- -----------------------------------
-- -----------------------------------
--Clients seen by DHB/non-DHB
SELECT year(IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_start_date) AS StartYear,
case when IDI_Metadata.clean_read_CLASSIFICATIONS.moh_primhd_organisation_code.ORGANISATION_TYPE = 'District Health Board (DHB)'then 'DHB'
else 'non-DHB' end 'Organisation type',
count (DISTINCT snz_moh_uid) AS 'Clients seen'
FROM
IDI_Clean_20181020.moh_clean.PRIMHD
INNER JOIN IDI_Metadata.clean_read_CLASSIFICATIONS.moh_primhd_organisation_code on
IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_organisation_id_code = IDI_Metadata.clean_read_CLASSIFICATIONS.moh_primhd_organisation_code.ORGANISATION_ID
WHERE
IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_type_code not in ('T35','T32','T33','T37', 'T08')
AND IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_setting_code not in ('WR','PH','SM','OM')
GROUP BY
year(IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_start_date),
case when IDI_Metadata.clean_read_CLASSIFICATIONS.moh_primhd_organisation_code.ORGANISATION_TYPE = 'District Health Board (DHB)'then 'DHB'
else 'non-DHB' end
ORDER BY
year(IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_start_date )
-- -----------------------------------
-- -----------------------------------
--Contacts
--AoD Contacts/all contacts
SELECT
year(IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_start_date) AS StartYear,
sum (case when (IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_team_type_code in ('03', '11') or
IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_type_code in ('T16', 'T17', 'T18', 'T19', 'T20','T48'))
then IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_unit_count_nbr else NULL end) AS 'AoD Contacts',
sum(IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_unit_count_nbr) 'All Contacts'
FROM
IDI_Clean_20181020.moh_clean.PRIMHD
WHERE
IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_type_code != 'T35'
AND
IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_unit_type_text = 'CON'
GROUP BY
year(IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_start_date)
ORDER BY
year(IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_start_date)
-- -----------------------------------
-- -----------------------------------
--All face to face contacts/AoD face to face contacts
SELECT year(IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_start_date) AS StartYear,
sum (IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_unit_count_nbr) AS 'All F2F contacts',
sum (case when (IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_team_type_code in ('03', '11') or
IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_type_code in ('T16', 'T17', 'T18', 'T19', 'T20','T48')) then
IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_unit_count_nbr else NULL end) AS 'AoD F2F Contacts'
FROM
IDI_Clean_20181020.moh_clean.PRIMHD
WHERE
IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_type_code not in ('T35','T32','T33','T37', 'T08')
AND IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_setting_code not in ('WR','PH','SM','OM')
and IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_unit_type_text = 'CON'
GROUP BY
year(IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_start_date);
-- -----------------------------------
-- -----------------------------------
--People with face to face contacts (snz_uid)
SELECT distinct(snz_moh_uid),
year(IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_start_date) AS StartYear
FROM
IDI_Clean_20181020.moh_clean.PRIMHD
WHERE
IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_type_code not in ('T35','T32','T33','T37', 'T08')
AND IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_setting_code not in ('WR','PH','SM','OM')
AND IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_unit_type_text = 'CON'
ORDER BY
year(IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_start_date);
-- -----------------------------------
-- -----------------------------------
--People with face to face contacts by DHB/non-DHB
SELECT year(IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_start_date) AS StartYear,
case when IDI_Metadata.clean_read_CLASSIFICATIONS.moh_primhd_organisation_code.ORGANISATION_NAME LIKE '%District Health Board%' then 'DHB' else 'non-DHB' end 'Organisation type',
count (DISTINCT snz_moh_uid) AS 'Service users face to face contacts'
FROM
IDI_Clean_20181020.moh_clean.PRIMHD
INNER JOIN IDI_Metadata.clean_read_CLASSIFICATIONS.moh_primhd_organisation_code on
IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_organisation_id_code = IDI_Metadata.clean_read_CLASSIFICATIONS.moh_primhd_organisation_code.ORGANISATION_ID
WHERE
IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_type_code not in ('T35','T32','T33','T37', 'T08')
AND IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_setting_code not in ('WR','PH','SM','OM')
AND IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_unit_type_text = 'CON'
GROUP BY
year(IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_start_date),
case when IDI_Metadata.clean_read_CLASSIFICATIONS.moh_primhd_organisation_code.ORGANISATION_NAME LIKE '%District Health Board%' then 'DHB' else 'non-DHB' end
ORDER BY
year(IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_start_date)
-- -----------------------------------
-- -----------------------------------
--Bednights
Sum of bednights/AoD bednights
SELECT
year(IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_start_date) AS StartYear,
sum (case when (IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_team_type_code in ('03', '11') or
IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_type_code in ('T16', 'T17', 'T18', 'T19', 'T20','T48'))
then IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_unit_count_nbr else NULL end) AS 'AoD_Bed_Nights',
sum(IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_unit_count_nbr) 'All Bed Nights'
FROM
IDI_Clean_20181020.moh_clean.PRIMHD
WHERE
IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_unit_type_text = 'BED'
GROUP BY
year(IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_start_date)
ORDER BY
year(IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_start_date);
-- -----------------------------------
-- -----------------------------------
--Bednights by snz_moh_uid
SELECT
year(IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_start_date) AS StartYear,
IDI_Clean_20181020.moh_clean.PRIMHD.snz_moh_uid,
sum(IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_unit_count_nbr) 'Bed Nights'
FROM
IDI_Clean_20181020.moh_clean.PRIMHD
WHERE
IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_type_code != 'T35'
AND
IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_unit_type_text = 'BED'
GROUP BY
year(IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_start_date),
IDI_Clean_20181020.moh_clean.PRIMHD.snz_moh_uid
ORDER BY
year(IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_start_date)
-- -----------------------------------
-- -----------------------------------
--Bednights by DHB/non-DHB
SELECT
year(IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_start_date) AS StartYear,
case when IDI_Metadata.clean_read_CLASSIFICATIONS.moh_primhd_organisation_code.ORGANISATION_NAME LIKE '%District Health Board%' then 'DHB' else 'non-DHB' end 'Organisation type',
sum(IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_unit_count_nbr) 'Bed Nights'
FROM
IDI_Clean_20181020.moh_clean.PRIMHD
INNER JOIN IDI_Metadata.clean_read_CLASSIFICATIONS.moh_primhd_organisation_code on
IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_organisation_id_code = IDI_Metadata.clean_read_CLASSIFICATIONS.moh_primhd_organisation_code.ORGANISATION_ID
WHERE
IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_type_code != 'T35'
AND
IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_unit_type_text = 'BED'
GROUP BY
year(IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_start_date),
case when IDI_Metadata.clean_read_CLASSIFICATIONS.moh_primhd_organisation_code.ORGANISATION_NAME LIKE '%District Health Board%' then 'DHB' else 'non-DHB' end
ORDER BY
year(IDI_Clean_20181020.moh_clean.PRIMHD.moh_mhd_activity_start_date)