-
Notifications
You must be signed in to change notification settings - Fork 0
/
principal_diagnosis.sql
1574 lines (1461 loc) · 54.7 KB
/
principal_diagnosis.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- Data import
USE
ICD_10_DE;
-- Import principal diagnosis data for Germany
-- Drop the table if it exists
DROP TABLE
IF EXISTS diagnoses_DE;
-- Create the table with utf8mb4 character set
CREATE TABLE
diagnoses_DE (
ICD_10 VARCHAR(255),
ICD_10_description VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci, -- mySQL default encoding doesn't support 'Umlaute'
`2012` INT,
`2013` INT,
`2014` INT,
`2015` INT,
`2016` INT,
`2017` INT,
`2018` INT,
`2019` INT,
`2020` INT,
`2021` INT
);
-- Load data from CSV
LOAD DATA INFILE 'C:\\ProgramData\\MySQL\\MySQL Server 8.1\\Uploads\\src\\23131-0001_$F.csv' INTO TABLE diagnoses_DE
FIELDS TERMINATED BY ';' ENCLOSED BY '"' LINES TERMINATED BY '\n' IGNORE 7 ROWS (
ICD_10,
@ICD_10_description,
@`2012`,
@`2013`,
@`2014`,
@`2015`,
@`2016`,
@`2017`,
@`2018`,
@`2019`,
@`2020`,
@`2021`
)
SET
ICD_10_description = CONVERT(REPLACE(REPLACE(REPLACE(@ICD_10_description, '\xF6', 'oe'), '\xFC', 'ue'), '\xE4', 'ae'), CHAR),
`2012` = NULLIF(@`2012`, '-'),
`2013` = NULLIF(@`2013`, '-'),
`2014` = NULLIF(@`2014`, '-'),
`2015` = NULLIF(@`2015`, '-'),
`2016` = NULLIF(@`2016`, '-'),
`2017` = NULLIF(@`2017`, '-'),
`2018` = NULLIF(@`2018`, '-'),
`2019` = NULLIF(@`2019`, '-'),
`2020` = NULLIF(@`2020`, '-'),
`2021` = CASE
WHEN TRIM(@`2021`) REGEXP '^[0-9]*\\.?[0-9]+$' THEN TRIM(@`2021`)
ELSE NULL
END;
-- Import population data for germany
-- Drop the table if it exists
DROP TABLE
IF EXISTS population_DE;
CREATE TABLE population_DE(
`year` YEAR,
population INT
);
-- Load data from CSV
LOAD DATA INFILE 'C:\\ProgramData\\MySQL\\MySQL Server 8.1\\Uploads\\src\\DE_population.csv' INTO TABLE population_DE
FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n' IGNORE 1 ROWS (
`Year`,
@population
)
SET
population = @population*1000000;
-- Import principal diagnosis data for USA
-- Drop the table if it exists
DROP TABLE IF EXISTS diagnoses_US;
-- Create the table with utf8mb4 character set
CREATE TABLE diagnoses_US (
ICD_10 VARCHAR(255),
ICD_10_description VARCHAR(255),
`2020` INT,
`2019` INT,
`2018` INT,
`2017` INT,
`2016` INT
);
-- Load data from CSV, skipping the first 4 lines (metadata and headers)
LOAD DATA INFILE 'C:\\ProgramData\\MySQL\\MySQL Server 8.1\\Uploads\\src\\HCUP-NIS2016-2020-DXandPRfreqs.csv' INTO TABLE diagnoses_US
FIELDS TERMINATED BY '\t' ENCLOSED BY '"' LINES TERMINATED BY '\n' IGNORE 8 LINES (
ICD_10,
ICD_10_description,
@C,
@D,
@`2020`,
@F,
@G,
@H,
@`2019`,
@J,
@K,
@L,
@`2018`,
@N,
@O,
@P,
@`2017`,
@R,
@S,
@T,
@`2016`,
@V,
@W,
@X
)
SET
`2020` = NULLIF(REPLACE(REPLACE(@`2020`, '*', ''), ' ', ''), ''),
`2019` = NULLIF(REPLACE(REPLACE(@`2019`, '*', ''), ' ', ''), ''),
`2018` = NULLIF(REPLACE(REPLACE(@`2018`, '*', ''), ' ', ''), ''),
`2017` = NULLIF(REPLACE(REPLACE(@`2017`, '*', ''), ' ', ''), ''),
`2016` = NULLIF(REPLACE(REPLACE(@`2016`, '*', ''), ' ', ''), '');
-- Import population data for USA
DROP TABLE IF EXISTS population_US;
CREATE TABLE population_US (
`Year` YEAR,
Population INT
);
-- Load data from CSV
LOAD DATA INFILE 'C:\\ProgramData\\MySQL\\MySQL Server 8.1\\Uploads\\src\\NST-EST2020.csv' INTO TABLE population_US
FIELDS TERMINATED BY ';' LINES TERMINATED BY '\n' IGNORE 1 ROWS (
`Year`,
@Population
)
SET
Population = REPLACE(@Population, ' ', '');
-------------
-- confirm the import by listing the column names
SHOW COLUMNS
FROM diagnoses_DE;
SHOW COLUMNS
FROM diagnoses_US;
SELECT * FROM
population_DE;
SELECT * FROM
population_US;
-- Select all groups of diseases
SELECT *
FROM diagnoses_DE
WHERE ICD_10 LIKE '%-%-%'
ORDER BY `2012` DESC;
------------
-- DE data
-- Diagnosis Rate per year among all diagnoses (wide format)
SELECT
ICD_10,
ICD_10_description,
`2021` AS TotalCases2021,
(`2021` / (SELECT `2021` FROM diagnoses_DE WHERE ICD_10_description LIKE 'Insgesamt')) * 100 AS Diagnosis_Rate_2021,
(`2020` / (SELECT `2020` FROM diagnoses_DE WHERE ICD_10_description LIKE 'Insgesamt')) * 100 AS Diagnosis_Rate_2020,
(`2019` / (SELECT `2019` FROM diagnoses_DE WHERE ICD_10_description LIKE 'Insgesamt')) * 100 AS Diagnosis_Rate_2019,
(`2018` / (SELECT `2018` FROM diagnoses_DE WHERE ICD_10_description LIKE 'Insgesamt')) * 100 AS Diagnosis_Rate_2018,
(`2017` / (SELECT `2017` FROM diagnoses_DE WHERE ICD_10_description LIKE 'Insgesamt')) * 100 AS Diagnosis_Rate_2017,
(`2016` / (SELECT `2016` FROM diagnoses_DE WHERE ICD_10_description LIKE 'Insgesamt')) * 100 AS Diagnosis_Rate_2016,
(`2015` / (SELECT `2015` FROM diagnoses_DE WHERE ICD_10_description LIKE 'Insgesamt')) * 100 AS Diagnosis_Rate_2015,
(`2014` / (SELECT `2014` FROM diagnoses_DE WHERE ICD_10_description LIKE 'Insgesamt')) * 100 AS Diagnosis_Rate_2014,
(`2013` / (SELECT `2013` FROM diagnoses_DE WHERE ICD_10_description LIKE 'Insgesamt')) * 100 AS Diagnosis_Rate_2013,
(`2012` / (SELECT `2012` FROM diagnoses_DE WHERE ICD_10_description LIKE 'Insgesamt')) * 100 AS Diagnosis_Rate_2012
FROM
diagnoses_DE
WHERE ICD_10 NOT LIKE '%-%-%'
ORDER BY Diagnosis_Rate_2021 DESC;
-- Create a new view to melt the data (long format)
DROP VIEW IF EXISTS melted_diagnoses_rate_DE;
CREATE VIEW melted_diagnoses_rate_DE AS
SELECT
ICD_10,
ICD_10_description,
'2021' AS Year,
`2021` AS Cases,
(`2021` / (SELECT `2021` FROM diagnoses_DE WHERE ICD_10_description LIKE 'Insgesamt')) * 100 AS Diagnosis_Rate
FROM diagnoses_DE
WHERE ICD_10 NOT LIKE '%-%-%'
UNION ALL
SELECT
ICD_10,
ICD_10_description,
'2020' AS Year,
`2020` AS Cases,
(`2020` / (SELECT `2020` FROM diagnoses_DE WHERE ICD_10_description LIKE 'Insgesamt')) * 100 AS Diagnosis_Rate
FROM diagnoses_DE
WHERE ICD_10 NOT LIKE '%-%-%'
UNION ALL
SELECT
ICD_10,
ICD_10_description,
'2019' AS Year,
`2019` AS Cases,
(`2019` / (SELECT `2019` FROM diagnoses_DE WHERE ICD_10_description LIKE 'Insgesamt')) * 100 AS Diagnosis_Rate
FROM diagnoses_DE
WHERE ICD_10 NOT LIKE '%-%-%'
UNION ALL
SELECT
ICD_10,
ICD_10_description,
'2018' AS Year,
`2018` AS Cases,
(`2018` / (SELECT `2018` FROM diagnoses_DE WHERE ICD_10_description LIKE 'Insgesamt')) * 100 AS Diagnosis_Rate
FROM diagnoses_DE
WHERE ICD_10 NOT LIKE '%-%-%'
UNION ALL
SELECT
ICD_10,
ICD_10_description,
'2017' AS Year,
`2017` AS Cases,
(`2017` / (SELECT `2017` FROM diagnoses_DE WHERE ICD_10_description LIKE 'Insgesamt')) * 100 AS Diagnosis_Rate
FROM diagnoses_DE
WHERE ICD_10 NOT LIKE '%-%-%'
UNION ALL
SELECT
ICD_10,
ICD_10_description,
'2016' AS Year,
`2016` AS Cases,
(`2016` / (SELECT `2016` FROM diagnoses_DE WHERE ICD_10_description LIKE 'Insgesamt')) * 100 AS Diagnosis_Rate
FROM diagnoses_DE
WHERE ICD_10 NOT LIKE '%-%-%'
UNION ALL
SELECT
ICD_10,
ICD_10_description,
'2015' AS Year,
`2015` AS Cases,
(`2015` / (SELECT `2015` FROM diagnoses_DE WHERE ICD_10_description LIKE 'Insgesamt')) * 100 AS Diagnosis_Rate
FROM diagnoses_DE
WHERE ICD_10 NOT LIKE '%-%-%'
UNION ALL
SELECT
ICD_10,
ICD_10_description,
'2014' AS Year,
`2014` AS Cases,
(`2014` / (SELECT `2014` FROM diagnoses_DE WHERE ICD_10_description LIKE 'Insgesamt')) * 100 AS Diagnosis_Rate
FROM diagnoses_DE
WHERE ICD_10 NOT LIKE '%-%-%'
UNION ALL
SELECT
ICD_10,
ICD_10_description,
'2013' AS Year,
`2013` AS Cases,
(`2013` / (SELECT `2013` FROM diagnoses_DE WHERE ICD_10_description LIKE 'Insgesamt')) * 100 AS Diagnosis_Rate
FROM diagnoses_DE
WHERE ICD_10 NOT LIKE '%-%-%'
UNION ALL
SELECT
ICD_10,
ICD_10_description,
'2012' AS Year,
`2012` AS Cases,
(`2012` / (SELECT `2012` FROM diagnoses_DE WHERE ICD_10_description LIKE 'Insgesamt')) * 100 AS Diagnosis_Rate
FROM diagnoses_DE
WHERE ICD_10 NOT LIKE '%-%-%'
ORDER BY Year, ICD_10;
/* WITH RankedData AS (
SELECT
ICD_10,
ICD_10_description,
Year,
Cases,
Diagnosis_Rate,
ROW_NUMBER() OVER (PARTITION BY Year ORDER BY Cases DESC) AS RowNum
FROM melted_diagnoses_rate_DE
)
SELECT
ICD_10,
ICD_10_description,
Year,
Cases,
Diagnosis_Rate
FROM RankedData
WHERE RowNum <= 100; */
SELECT *
FROM melted_diagnoses_rate_DE
WHERE Cases IS NOT NULL;
-- Proportion of Diagnoses per 100k people per year (wide format)
SELECT
d.ICD_10,
d.ICD_10_description,
`2021` AS TotalCases2021,
(d.2021 / P2021.Population) * 100000 AS Cases_per_100k_2021,
(d.2020 / P2020.Population) * 100000 AS Cases_per_100k_2020,
(d.2019 / P2019.Population) * 100000 AS Cases_per_100k_2019,
(d.2018 / P2018.Population) * 100000 AS Cases_per_100k_2018,
(d.2017 / P2017.Population) * 100000 AS Cases_per_100k_2017,
(d.2016 / P2016.Population) * 100000 AS Cases_per_100k_2016,
(d.2015 / P2015.Population) * 100000 AS Cases_per_100k_2015,
(d.2014 / P2014.Population) * 100000 AS Cases_per_100k_2014,
(d.2013 / P2013.Population) * 100000 AS Cases_per_100k_2013,
(d.2012 / P2012.Population) * 100000 AS Cases_per_100k_2012
FROM
diagnoses_DE d
JOIN
population_DE AS P2021 ON P2021.Year = 2021
JOIN
population_DE AS P2020 ON P2020.Year = 2020
JOIN
population_DE AS P2019 ON P2019.Year = 2019
JOIN
population_DE AS P2018 ON P2018.Year = 2018
JOIN
population_DE AS P2017 ON P2017.Year = 2017
JOIN
population_DE AS P2016 ON P2016.Year = 2016
JOIN
population_DE AS P2015 ON P2015.Year = 2015
JOIN
population_DE AS P2014 ON P2014.Year = 2014
JOIN
population_DE AS P2013 ON P2013.Year = 2013
JOIN
population_DE AS P2012 ON P2012.Year = 2012
WHERE ICD_10 NOT LIKE '%-%-%'
ORDER BY Cases_per_100k_2021 DESC;
-- Create a view to melt the data (long format)
DROP VIEW IF EXISTS melted_diagnoses_per_100k_DE;
CREATE VIEW melted_diagnoses_per_100k_DE AS
SELECT
'DE' AS Country,
d.ICD_10,
d.ICD_10_description,
'2021' AS Year,
d.`2021` AS Cases,
(d.`2021` / p2021.Population) * 100000 AS Cases_per_100k
FROM diagnoses_DE d
JOIN population_DE p2021 ON p2021.Year = 2021
WHERE d.ICD_10 NOT LIKE '%-%-%'
AND d.ICD_10_description NOT LIKE 'Insgesamt'
UNION ALL
SELECT
'DE' AS Country,
d.ICD_10,
d.ICD_10_description,
'2020' AS Year,
d.`2020` AS Cases,
(d.`2020` / p2020.Population) * 100000 AS Cases_per_100k
FROM diagnoses_DE d
JOIN population_DE p2020 ON p2020.Year = 2020
WHERE d.ICD_10 NOT LIKE '%-%-%'
UNION ALL
SELECT
'DE' AS Country,
d.ICD_10,
d.ICD_10_description,
'2019' AS Year,
d.`2019` AS Cases,
(d.`2019` / p2019.Population) * 100000 AS Cases_per_100k
FROM diagnoses_DE d
JOIN population_DE p2019 ON p2019.Year = 2019
WHERE d.ICD_10 NOT LIKE '%-%-%'
UNION ALL
SELECT
'DE' AS Country,
d.ICD_10,
d.ICD_10_description,
'2018' AS Year,
d.`2018` AS Cases,
(d.`2018` / p2018.Population) * 100000 AS Cases_per_100k
FROM diagnoses_DE d
JOIN population_DE p2018 ON p2018.Year = 2018
WHERE d.ICD_10 NOT LIKE '%-%-%'
UNION ALL
SELECT
'DE' AS Country,
d.ICD_10,
d.ICD_10_description,
'2017' AS Year,
d.`2017` AS Cases,
(d.`2017` / p2017.Population) * 100000 AS Cases_per_100k
FROM diagnoses_DE d
JOIN population_DE p2017 ON p2017.Year = 2017
WHERE d.ICD_10 NOT LIKE '%-%-%'
UNION ALL
SELECT
'DE' AS Country,
d.ICD_10,
d.ICD_10_description,
'2016' AS Year,
d.`2016` AS Cases,
(d.`2016` / p2016.Population) * 100000 AS Cases_per_100k
FROM diagnoses_DE d
JOIN population_DE p2016 ON p2016.Year = 2016
WHERE d.ICD_10 NOT LIKE '%-%-%'
UNION ALL
SELECT
'DE' AS Country,
d.ICD_10,
d.ICD_10_description,
'2015' AS Year,
d.`2015` AS Cases,
(d.`2015` / p2015.Population) * 100000 AS Cases_per_100k
FROM diagnoses_DE d
JOIN population_DE p2015 ON p2015.Year = 2015
WHERE d.ICD_10 NOT LIKE '%-%-%'
UNION ALL
SELECT
'DE' AS Country,
d.ICD_10,
d.ICD_10_description,
'2014' AS Year,
d.`2014` AS Cases,
(d.`2014` / p2014.Population) * 100000 AS Cases_per_100k
FROM diagnoses_DE d
JOIN population_DE p2014 ON p2014.Year = 2014
WHERE d.ICD_10 NOT LIKE '%-%-%'
UNION ALL
SELECT
'DE' AS Country,
d.ICD_10,
d.ICD_10_description,
'2013' AS Year,
d.`2013` AS Cases,
(d.`2013` / p2013.Population) * 100000 AS Cases_per_100k
FROM diagnoses_DE d
JOIN population_DE p2013 ON p2013.Year = 2013
WHERE d.ICD_10 NOT LIKE '%-%-%'
UNION ALL
SELECT
'DE' AS Country,
d.ICD_10,
d.ICD_10_description,
'2012' AS Year,
d.`2012` AS Cases,
(d.`2012` / p2012.Population) * 100000 AS Cases_per_100k
FROM diagnoses_DE d
JOIN population_DE p2012 ON p2012.Year = 2012
WHERE d.ICD_10 NOT LIKE '%-%-%'
ORDER BY Year, Cases_per_100k DESC;
/* WITH RankedData AS (
SELECT
ICD_10,
ICD_10_description,
Year,
Cases,
Cases_per_100k,
ROW_NUMBER() OVER (PARTITION BY Year ORDER BY Cases DESC) AS RowNum
FROM melted_diagnoses_per_100k_DE
)
SELECT
ICD_10,
ICD_10_description,
Year,
Cases,
Cases_per_100k
FROM RankedData
WHERE RowNum <= 100; */
SELECT *
FROM melted_diagnoses_per_100k_DE
WHERE Cases IS NOT NULL;
-- Select diagnoses that increased the most from 2012 to 2021 in relation to the population
SELECT
D.ICD_10,
D.ICD_10_description,
((D.2021 / P2021.population - D.2012 / P2012.population) / (D.2012 / P2012.population)) * 100 AS Percentage_Increase_Relative_to_Population
FROM diagnoses_DE AS D
JOIN population_DE AS P2021 ON P2021.Year = 2021
JOIN population_DE AS P2012 ON P2012.Year = 2012
WHERE D.2021 > D.2012 AND ICD_10 NOT LIKE '%-%-%'
ORDER BY Percentage_Increase_Relative_to_Population DESC;
-- Select diagnosis that increased the most from 2012 to 2021 in relation to the population. Broad categories
SELECT
D.ICD_10,
D.ICD_10_description,
((D.2021 / P2021.population - D.2012 / P2012.population) / (D.2012 / P2012.population)) * 100 AS Percentage_Increase_Relative_to_Population
FROM diagnoses_DE AS D
JOIN population_DE AS P2021 ON P2021.Year = 2021
JOIN population_DE AS P2012 ON P2012.Year = 2012
WHERE D.2021 > D.2012 AND ICD_10 LIKE '%-%-%'
ORDER BY Percentage_Increase_Relative_to_Population DESC;
-- Select diagnosis that increased the most from 2016 to 2020 in relation to the population. Broad categories
SELECT
D.ICD_10,
D.ICD_10_description,
((D.2020 / P2020.population - D.2016 / P2016.population) / (D.2016 / P2016.population)) * 100 AS Percentage_Increase_Relative_to_Population
FROM diagnoses_DE AS D
JOIN population_DE AS P2020 ON P2020.Year = 2020
JOIN population_DE AS P2016 ON P2016.Year = 2016
WHERE D.2020 > D.2016 AND ICD_10 LIKE '%-%-%'
ORDER BY Percentage_Increase_Relative_to_Population DESC;
-- US DATA
-- Diagnosis Rate per year among all diagnoses (wide format)
SELECT
ICD_10,
ICD_10_description,
`2020` AS TotalCases2020,
(`2020` / (SELECT SUM(`2020`) FROM diagnoses_US))*100 AS Percentage2020,
(`2019` / (SELECT SUM(`2019`) FROM diagnoses_US))*100 AS Percentage2019,
(`2018` / (SELECT SUM(`2018`) FROM diagnoses_US))*100 AS Percentage2018,
(`2017` / (SELECT SUM(`2017`) FROM diagnoses_US))*100 AS Percentage2017,
(`2016` / (SELECT SUM(`2016`) FROM diagnoses_US))*100 AS Percentage2016
FROM
diagnoses_US
ORDER BY Percentage2020 DESC;
-- Create a view to melt the data (long format)
DROP VIEW IF EXISTS melted_diagnoses_rate_US;
CREATE VIEW melted_diagnoses_rate_US AS
SELECT
ICD_10,
ICD_10_description,
'2020' AS Year,
`2020` AS Cases,
(`2020` / (SELECT SUM(`2020`) FROM diagnoses_US)) * 100 AS Diagnosis_Rate
FROM diagnoses_US
UNION ALL
SELECT
ICD_10,
ICD_10_description,
'2019' AS Year,
`2019` AS Cases,
(`2019` / (SELECT SUM(`2019`) FROM diagnoses_US)) * 100 AS Diagnosis_Rate
FROM diagnoses_US
UNION ALL
SELECT
ICD_10,
ICD_10_description,
'2018' AS Year,
`2018` AS Cases,
(`2018` / (SELECT SUM(`2018`) FROM diagnoses_US)) * 100 AS Diagnosis_Rate
FROM diagnoses_US
UNION ALL
SELECT
ICD_10,
ICD_10_description,
'2017' AS Year,
`2017` AS Cases,
(`2017` / (SELECT SUM(`2017`) FROM diagnoses_US)) * 100 AS Diagnosis_Rate
FROM diagnoses_US
UNION ALL
SELECT
ICD_10,
ICD_10_description,
'2016' AS Year,
`2016` AS Cases,
(`2016` / (SELECT SUM(`2016`) FROM diagnoses_US)) * 100 AS Diagnosis_Rate
FROM diagnoses_US
ORDER BY Diagnosis_Rate;
/* WITH RankedData AS (
SELECT
ICD_10,
ICD_10_description,
Year,
Cases,
Diagnosis_Rate,
ROW_NUMBER() OVER (PARTITION BY Year ORDER BY Cases DESC) AS RowNum
FROM melted_diagnoses_rate_US
)
SELECT
ICD_10,
ICD_10_description,
Year,
Cases,
Diagnosis_Rate
FROM RankedData
WHERE RowNum <= 100; */
SELECT *
FROM melted_diagnoses_rate_US
WHERE Cases IS NOT NULL;
-- Proportion of Diagnoses per 100k people per year
SELECT
d.ICD_10,
d.ICD_10_description,
`2020` AS TotalCases2020,
(d.`2020` / P2020.Population) * 1000000 AS Cases_per_100k_2020,
(d.`2019` / p2019.Population) * 1000000 AS Cases_per_100k_2019,
(d.`2018` / p2018.Population) * 1000000 AS Cases_per_100k_2018,
(d.`2017` / p2017.Population) * 1000000 AS Cases_per_100k_2017,
(d.`2016` / p2016.Population) * 1000000 AS Cases_per_100k_2016
FROM
diagnoses_US d
JOIN
population_US AS P2020 ON P2020.Year = 2020
JOIN
population_US AS P2019 ON P2019.Year = 2019
JOIN
population_US AS P2018 ON P2018.Year = 2018
JOIN
population_US AS P2017 ON P2017.Year = 2017
JOIN
population_US AS P2016 ON P2016.Year = 2016
ORDER BY Cases_per_100k_2020 DESC;
-- Create a view to melt the data
DROP VIEW IF EXISTS melted_diagnoses_per_100k_US;
CREATE VIEW melted_diagnoses_per_100k_US AS
SELECT
'US' AS Country,
d.ICD_10,
d.ICD_10_description,
'2020' AS Year,
d.`2020` AS Cases,
(d.`2020` / p2020.Population) * 100000 AS Cases_per_100k
FROM diagnoses_US d
JOIN population_US p2020 ON p2020.Year = 2020
UNION ALL
SELECT
'US' AS Country,
d.ICD_10,
d.ICD_10_description,
'2019' AS Year,
d.`2019` AS Cases,
(d.`2019` / p2019.Population) * 100000 AS Cases_per_100k
FROM diagnoses_US d
JOIN population_US p2019 ON p2019.Year = 2019
UNION ALL
SELECT
'US' AS Country,
d.ICD_10,
d.ICD_10_description,
'2018' AS Year,
d.`2018` AS Cases,
(d.`2018` / p2018.Population) * 100000 AS Cases_per_100k
FROM diagnoses_US d
JOIN population_US p2018 ON p2018.Year = 2018
UNION ALL
SELECT
'US' AS Country,
d.ICD_10,
d.ICD_10_description,
'2017' AS Year,
d.`2017` AS Cases,
(d.`2017` / p2017.Population) * 100000 AS Cases_per_100k
FROM diagnoses_US d
JOIN population_US p2017 ON p2017.Year = 2017
UNION ALL
SELECT
'US' AS Country,
d.ICD_10,
d.ICD_10_description,
'2016' AS Year,
d.`2016` AS Cases,
(d.`2016` / p2016.Population) * 100000 AS Cases_per_100k
FROM diagnoses_US d
JOIN population_US p2016 ON p2016.Year = 2016
ORDER BY Year, Cases_per_100k DESC;
/* WITH RankedData AS (
SELECT
ICD_10,
ICD_10_description,
Year,
Cases,
Cases_per_100k,
ROW_NUMBER() OVER (PARTITION BY Year ORDER BY Cases DESC) AS RowNum
FROM melted_diagnoses_per_100k_US
)
SELECT
ICD_10,
ICD_10_description,
Year,
Cases,
Cases_per_100k
FROM RankedData
WHERE RowNum <= 100; */
SELECT *
FROM melted_diagnoses_per_100k_US
WHERE Cases IS NOT NULL;
-- Select diagnoses that increased the most from 2016 to 2020 in relation to the population
SELECT
D.ICD_10,
D.ICD_10_description,
((D.`2020` / P2020.population - D.`2016` / P2016.population) / (D.`2016` / P2016.population)) * 100 AS Percentage_Increase_Relative_to_Population
FROM diagnoses_US AS D
JOIN population_US AS P2020 ON P2020.Year = 2020
JOIN population_US AS P2016 ON P2016.Year = 2016
WHERE D.`2020` > D.`2016`
ORDER BY Percentage_Increase_Relative_to_Population DESC
LIMIT 100;
-- Zeitlicher Verlauf ausgewählter Erkrankungen
-- ICD10-E00-E90 Endokrine, Ernährungs- und Stoffwechselkrankheiten
-- Create a view to melt the data
DROP VIEW IF EXISTS melted_endocrine_data;
CREATE VIEW melted_endocrine_data AS
SELECT
'Endocrine' AS Description,
'US' AS Country,
2020 AS Year,
((SELECT SUM(`2020`) FROM diagnoses_US WHERE ICD_10 LIKE 'E%' AND Year = 2020) / (SELECT SUM(`2020`) FROM diagnoses_US )) * 100 AS Percentage
UNION ALL
SELECT
'Endocrine' AS Description,
'US' AS Country,
2019 AS Year,
((SELECT SUM(`2019`) FROM diagnoses_US WHERE ICD_10 LIKE 'E%' AND Year = 2019) / (SELECT SUM(`2019`) FROM diagnoses_US)) * 100 AS Percentage
UNION ALL
SELECT
'Endocrine' AS Description,
'US' AS Country,
2018 AS Year,
((SELECT SUM(`2018`) FROM diagnoses_US WHERE ICD_10 LIKE 'E%' AND Year = 2018) / (SELECT SUM(`2018`) FROM diagnoses_US)) * 100 AS Percentage
UNION ALL
SELECT
'Endocrine' AS Description,
'US' AS Country,
2017 AS Year,
((SELECT SUM(`2017`) FROM diagnoses_US WHERE ICD_10 LIKE 'E%' AND Year = 2017) / (SELECT SUM(`2017`) FROM diagnoses_US)) * 100 AS Percentage
UNION ALL
SELECT
'Endocrine' AS Description,
'US' AS Country,
2016 AS Year,
((SELECT SUM(`2016`) FROM diagnoses_US WHERE ICD_10 LIKE 'E%' AND Year = 2016) / (SELECT SUM(`2016`) FROM diagnoses_US)) * 100 AS Percentage
UNION ALL
SELECT
'Endocrine' AS Description,
'DE' AS Country,
2020 AS Year,
((SELECT SUM(`2020`) FROM diagnoses_DE WHERE ICD_10 LIKE 'ICD10-E%' AND ICD_10 NOT LIKE '%-%-%') / (SELECT `2020` FROM diagnoses_DE WHERE ICD_10_description LIKE 'Insgesamt')) * 100 AS Percentage
UNION ALL
SELECT
'Endocrine' AS Description,
'DE' AS Country,
2019 AS Year,
((SELECT SUM(`2019`) FROM diagnoses_DE WHERE ICD_10 LIKE 'ICD10-E%' AND ICD_10 NOT LIKE '%-%-%') / (SELECT `2019` FROM diagnoses_DE WHERE ICD_10_description LIKE 'Insgesamt')) * 100 AS Percentage
UNION ALL
SELECT
'Endocrine' AS Description,
'DE' AS Country,
2018 AS Year,
((SELECT SUM(`2018`) FROM diagnoses_DE WHERE ICD_10 LIKE 'ICD10-E%' AND ICD_10 NOT LIKE '%-%-%') / (SELECT `2018` FROM diagnoses_DE WHERE ICD_10_description LIKE 'Insgesamt')) * 100 AS Percentage
UNION ALL
SELECT
'Endocrine' AS Description,
'DE' AS Country,
2017 AS Year,
((SELECT SUM(`2017`) FROM diagnoses_DE WHERE ICD_10 LIKE 'ICD10-E%' AND ICD_10 NOT LIKE '%-%-%') / (SELECT `2017` FROM diagnoses_DE WHERE ICD_10_description LIKE 'Insgesamt')) * 100 AS Percentage
UNION ALL
SELECT
'Endocrine' AS Description,
'DE' AS Country,
2016 AS Year,
((SELECT SUM(`2016`) FROM diagnoses_DE WHERE ICD_10 LIKE 'ICD10-E%' AND ICD_10 NOT LIKE '%-%-%') / (SELECT `2016` FROM diagnoses_DE WHERE ICD_10_description LIKE 'Insgesamt')) * 100 AS Percentage;
SELECT *
FROM melted_endocrine_data;
-- Krankheiten des Atmungssystems
-- Create a view to melt the data
DROP VIEW IF EXISTS melted_respiratory_data;
CREATE VIEW melted_respiratory_data AS
SELECT
'Respiratory' AS Description,
'US' AS Country,
2020 AS Year,
((SELECT SUM(`2020`) FROM diagnoses_US WHERE ICD_10 LIKE 'J%' AND Year = 2020) / (SELECT SUM(`2020`) FROM diagnoses_US )) * 100 AS Percentage
UNION ALL
SELECT
'Respiratory' AS Description,
'US' AS Country,
2019 AS Year,
((SELECT SUM(`2019`) FROM diagnoses_US WHERE ICD_10 LIKE 'J%' AND Year = 2019) / (SELECT SUM(`2019`) FROM diagnoses_US)) * 100 AS Percentage
UNION ALL
SELECT
'Respiratory' AS Description,
'US' AS Country,
2018 AS Year,
((SELECT SUM(`2018`) FROM diagnoses_US WHERE ICD_10 LIKE 'J%' AND Year = 2018) / (SELECT SUM(`2018`) FROM diagnoses_US)) * 100 AS Percentage
UNION ALL
SELECT
'Respiratory' AS Description,
'US' AS Country,
2017 AS Year,
((SELECT SUM(`2017`) FROM diagnoses_US WHERE ICD_10 LIKE 'J%' AND Year = 2017) / (SELECT SUM(`2017`) FROM diagnoses_US)) * 100 AS Percentage
UNION ALL
SELECT
'Respiratory' AS Description,
'US' AS Country,
2016 AS Year,
((SELECT SUM(`2016`) FROM diagnoses_US WHERE ICD_10 LIKE 'J%' AND Year = 2016) / (SELECT SUM(`2016`) FROM diagnoses_US)) * 100 AS Percentage
UNION ALL
SELECT
'Respiratory' AS Description,
'DE' AS Country,
2020 AS Year,
((SELECT SUM(`2020`) FROM diagnoses_DE WHERE ICD_10 LIKE 'ICD10-J%' AND ICD_10 NOT LIKE '%-%-%') / (SELECT `2020` FROM diagnoses_DE WHERE ICD_10_description LIKE 'Insgesamt')) * 100 AS Percentage
UNION ALL
SELECT
'Respiratory' AS Description,
'DE' AS Country,
2019 AS Year,
((SELECT SUM(`2019`) FROM diagnoses_DE WHERE ICD_10 LIKE 'ICD10-J%' AND ICD_10 NOT LIKE '%-%-%') / (SELECT `2019` FROM diagnoses_DE WHERE ICD_10_description LIKE 'Insgesamt')) * 100 AS Percentage
UNION ALL
SELECT
'Respiratory' AS Description,
'DE' AS Country,
2018 AS Year,
((SELECT SUM(`2018`) FROM diagnoses_DE WHERE ICD_10 LIKE 'ICD10-J%' AND ICD_10 NOT LIKE '%-%-%') / (SELECT `2018` FROM diagnoses_DE WHERE ICD_10_description LIKE 'Insgesamt')) * 100 AS Percentage
UNION ALL
SELECT
'Respiratory' AS Description,
'DE' AS Country,
2017 AS Year,
((SELECT SUM(`2017`) FROM diagnoses_DE WHERE ICD_10 LIKE 'ICD10-J%' AND ICD_10 NOT LIKE '%-%-%') / (SELECT `2017` FROM diagnoses_DE WHERE ICD_10_description LIKE 'Insgesamt')) * 100 AS Percentage
UNION ALL
SELECT
'Respiratory' AS Description,
'DE' AS Country,
2016 AS Year,
((SELECT SUM(`2016`) FROM diagnoses_DE WHERE ICD_10 LIKE 'ICD10-J%' AND ICD_10 NOT LIKE '%-%-%') / (SELECT `2016` FROM diagnoses_DE WHERE ICD_10_description LIKE 'Insgesamt')) * 100 AS Percentage;
SELECT *
FROM melted_respiratory_data;
-- Krankheiten des Kreislaufsystems
-- Create a view to melt the data
DROP VIEW IF EXISTS melted_circulatory_data;
CREATE VIEW melted_circulatory_data AS
SELECT
'Circulatory' AS Description,
'US' AS Country,
2020 AS Year,
((SELECT SUM(`2020`) FROM diagnoses_US WHERE ICD_10 LIKE 'I%' AND Year = 2020) / (SELECT SUM(`2020`) FROM diagnoses_US )) * 100 AS Percentage
UNION ALL
SELECT
'Circulatory' AS Description,
'US' AS Country,
2019 AS Year,
((SELECT SUM(`2019`) FROM diagnoses_US WHERE ICD_10 LIKE 'I%' AND Year = 2019) / (SELECT SUM(`2019`) FROM diagnoses_US)) * 100 AS Percentage
UNION ALL
SELECT
'Circulatory' AS Description,
'US' AS Country,
2018 AS Year,
((SELECT SUM(`2018`) FROM diagnoses_US WHERE ICD_10 LIKE 'I%' AND Year = 2018) / (SELECT SUM(`2018`) FROM diagnoses_US)) * 100 AS Percentage
UNION ALL
SELECT
'Circulatory' AS Description,
'US' AS Country,
2017 AS Year,
((SELECT SUM(`2017`) FROM diagnoses_US WHERE ICD_10 LIKE 'I%' AND Year = 2017) / (SELECT SUM(`2017`) FROM diagnoses_US)) * 100 AS Percentage
UNION ALL
SELECT
'Circulatory' AS Description,
'US' AS Country,
2016 AS Year,
((SELECT SUM(`2016`) FROM diagnoses_US WHERE ICD_10 LIKE 'I%' AND Year = 2016) / (SELECT SUM(`2016`) FROM diagnoses_US)) * 100 AS Percentage
UNION ALL
SELECT
'Circulatory' AS Description,
'DE' AS Country,
2020 AS Year,
((SELECT SUM(`2020`) FROM diagnoses_DE WHERE ICD_10 LIKE 'ICD10-I%' AND ICD_10 NOT LIKE '%-%-%') / (SELECT `2020` FROM diagnoses_DE WHERE ICD_10_description LIKE 'Insgesamt')) * 100 AS Percentage
UNION ALL
SELECT
'Circulatory' AS Description,
'DE' AS Country,
2019 AS Year,
((SELECT SUM(`2019`) FROM diagnoses_DE WHERE ICD_10 LIKE 'ICD10-I%' AND ICD_10 NOT LIKE '%-%-%') / (SELECT `2019` FROM diagnoses_DE WHERE ICD_10_description LIKE 'Insgesamt')) * 100 AS Percentage
UNION ALL
SELECT
'Circulatory' AS Description,
'DE' AS Country,
2018 AS Year,
((SELECT SUM(`2018`) FROM diagnoses_DE WHERE ICD_10 LIKE 'ICD10-I%' AND ICD_10 NOT LIKE '%-%-%') / (SELECT `2018` FROM diagnoses_DE WHERE ICD_10_description LIKE 'Insgesamt')) * 100 AS Percentage
UNION ALL
SELECT
'Circulatory' AS Description,
'DE' AS Country,
2017 AS Year,
((SELECT SUM(`2017`) FROM diagnoses_DE WHERE ICD_10 LIKE 'ICD10-I%' AND ICD_10 NOT LIKE '%-%-%') / (SELECT `2017` FROM diagnoses_DE WHERE ICD_10_description LIKE 'Insgesamt')) * 100 AS Percentage
UNION ALL
SELECT
'Circulatory' AS Description,
'DE' AS Country,
2016 AS Year,
((SELECT SUM(`2016`) FROM diagnoses_DE WHERE ICD_10 LIKE 'ICD10-I%' AND ICD_10 NOT LIKE '%-%-%') / (SELECT `2016` FROM diagnoses_DE WHERE ICD_10_description LIKE 'Insgesamt')) * 100 AS Percentage;
SELECT *
FROM melted_circulatory_data;
-- Psychische und Verhaltensstörungen
-- Create a view to melt the data
DROP VIEW IF EXISTS melted_mental_data;
CREATE VIEW melted_mental_data AS
SELECT
'Mental' AS Description,
'US' AS Country,
2020 AS Year,
((SELECT SUM(`2020`) FROM diagnoses_US WHERE ICD_10 LIKE 'F%' AND Year = 2020) / (SELECT SUM(`2020`) FROM diagnoses_US )) * 100 AS Percentage
UNION ALL
SELECT
'Mental' AS Description,
'US' AS Country,
2019 AS Year,
((SELECT SUM(`2019`) FROM diagnoses_US WHERE ICD_10 LIKE 'F%' AND Year = 2019) / (SELECT SUM(`2019`) FROM diagnoses_US)) * 100 AS Percentage
UNION ALL
SELECT
'Mental' AS Description,
'US' AS Country,
2018 AS Year,
((SELECT SUM(`2018`) FROM diagnoses_US WHERE ICD_10 LIKE 'F%' AND Year = 2018) / (SELECT SUM(`2018`) FROM diagnoses_US)) * 100 AS Percentage
UNION ALL
SELECT
'Mental' AS Description,
'US' AS Country,
2017 AS Year,
((SELECT SUM(`2017`) FROM diagnoses_US WHERE ICD_10 LIKE 'F%' AND Year = 2017) / (SELECT SUM(`2017`) FROM diagnoses_US)) * 100 AS Percentage
UNION ALL
SELECT
'Mental' AS Description,
'US' AS Country,
2016 AS Year,
((SELECT SUM(`2016`) FROM diagnoses_US WHERE ICD_10 LIKE 'F%' AND Year = 2016) / (SELECT SUM(`2016`) FROM diagnoses_US)) * 100 AS Percentage
UNION ALL
SELECT
'Mental' AS Description,
'DE' AS Country,
2020 AS Year,
((SELECT SUM(`2020`) FROM diagnoses_DE WHERE ICD_10 LIKE 'ICD10-F%' AND ICD_10 NOT LIKE '%-%-%') / (SELECT `2020` FROM diagnoses_DE WHERE ICD_10_description LIKE 'Insgesamt')) * 100 AS Percentage
UNION ALL
SELECT
'Mental' AS Description,
'DE' AS Country,
2019 AS Year,
((SELECT SUM(`2019`) FROM diagnoses_DE WHERE ICD_10 LIKE 'ICD10-F%' AND ICD_10 NOT LIKE '%-%-%') / (SELECT `2019` FROM diagnoses_DE WHERE ICD_10_description LIKE 'Insgesamt')) * 100 AS Percentage
UNION ALL
SELECT
'Mental' AS Description,
'DE' AS Country,
2018 AS Year,
((SELECT SUM(`2018`) FROM diagnoses_DE WHERE ICD_10 LIKE 'ICD10-F%' AND ICD_10 NOT LIKE '%-%-%') / (SELECT `2018` FROM diagnoses_DE WHERE ICD_10_description LIKE 'Insgesamt')) * 100 AS Percentage
UNION ALL
SELECT
'Mental' AS Description,
'DE' AS Country,