diff --git a/projects/066 - Heald (Long Covid)/extraction-sql/summary-stats.sql b/projects/066 - Heald (Long Covid)/extraction-sql/summary-stats.sql index baecaa77..aa3329a5 100644 --- a/projects/066 - Heald (Long Covid)/extraction-sql/summary-stats.sql +++ b/projects/066 - Heald (Long Covid)/extraction-sql/summary-stats.sql @@ -18,6 +18,335 @@ WHERE FK_Reference_Tenancy_ID=2 AND GPPracticeCode NOT LIKE 'ZZZ%'; -- 21s +--┌─────────────────────┐ +--│ Patients with COVID │ +--└─────────────────────┘ + +-- OBJECTIVE: To get tables of all patients with a COVID diagnosis in their record. This now includes a table +-- that has reinfections. This uses a 90 day cut-off to rule out patients that get multiple tests for +-- a single infection. This 90 day cut-off is also used in the government COVID dashboard. In the first wave, +-- prior to widespread COVID testing, and prior to the correct clinical codes being available to clinicians, +-- infections were recorded in a variety of ways. We therefore take the first diagnosis from any code indicative +-- of COVID. However, for subsequent infections we insist on the presence of a positive COVID test (PCR or antigen) +-- as opposed to simply a diagnosis code. This is to avoid the situation where a hospital diagnosis code gets +-- entered into the primary care record several months after the actual infection. + +-- INPUT: Takes three parameters +-- - start-date: string - (YYYY-MM-DD) the date to count diagnoses from. Usually this should be 2020-01-01. +-- - all-patients: boolean - (true/false) if true, then all patients are included, otherwise only those in the pre-existing #Patients table. +-- - gp-events-table: string - (table name) the name of the table containing the GP events. Usually is "SharedCare.GP_Events" but can be anything with the columns: FK_Patient_Link_ID, EventDate, and SuppliedCode + +-- OUTPUT: Three temp tables as follows: +-- #CovidPatients (FK_Patient_Link_ID, FirstCovidPositiveDate) +-- - FK_Patient_Link_ID - unique patient id +-- - FirstCovidPositiveDate - earliest COVID diagnosis +-- #CovidPatientsAllDiagnoses (FK_Patient_Link_ID, CovidPositiveDate) +-- - FK_Patient_Link_ID - unique patient id +-- - CovidPositiveDate - any COVID diagnosis +-- #CovidPatientsMultipleDiagnoses +-- - FK_Patient_Link_ID - unique patient id +-- - FirstCovidPositiveDate - date of first COVID diagnosis +-- - SecondCovidPositiveDate - date of second COVID diagnosis +-- - ThirdCovidPositiveDate - date of third COVID diagnosis +-- - FourthCovidPositiveDate - date of fourth COVID diagnosis +-- - FifthCovidPositiveDate - date of fifth COVID diagnosis + +-- >>> Codesets required... Inserting the code set code +-- +--┌────────────────────┐ +--│ Clinical code sets │ +--└────────────────────┘ + +-- OBJECTIVE: To populate temporary tables with the existing clinical code sets. +-- See the [SQL-generation-process.md](SQL-generation-process.md) for more details. + +-- INPUT: No pre-requisites + +-- OUTPUT: Five temp tables as follows: +-- #AllCodes (Concept, Version, Code) +-- #CodeSets (FK_Reference_Coding_ID, Concept) +-- #SnomedSets (FK_Reference_SnomedCT_ID, FK_SNOMED_ID) +-- #VersionedCodeSets (FK_Reference_Coding_ID, Concept, Version) +-- #VersionedSnomedSets (FK_Reference_SnomedCT_ID, Version, FK_SNOMED_ID) + +--!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! +--!!! DO NOT EDIT THIS FILE MANUALLY !!! +--!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +--#region Clinical code sets + +IF OBJECT_ID('tempdb..#AllCodes') IS NOT NULL DROP TABLE #AllCodes; +CREATE TABLE #AllCodes ( + [Concept] [varchar](255) NOT NULL, + [Version] INT NOT NULL, + [Code] [varchar](20) COLLATE Latin1_General_CS_AS NOT NULL, + [description] [varchar] (255) NULL +); + +IF OBJECT_ID('tempdb..#codesreadv2') IS NOT NULL DROP TABLE #codesreadv2; +CREATE TABLE #codesreadv2 ( + [concept] [varchar](255) NOT NULL, + [version] INT NOT NULL, + [code] [varchar](20) COLLATE Latin1_General_CS_AS NOT NULL, + [term] [varchar](20) COLLATE Latin1_General_CS_AS NULL, + [description] [varchar](255) NULL +) ON [PRIMARY]; + +INSERT INTO #codesreadv2 +VALUES ('covid-positive-antigen-test',1,'43kB1',NULL,'SARS-CoV-2 antigen positive'),('covid-positive-antigen-test',1,'43kB100',NULL,'SARS-CoV-2 antigen positive'); +INSERT INTO #codesreadv2 +VALUES ('covid-positive-pcr-test',1,'4J3R6',NULL,'SARS-CoV-2 RNA pos lim detect'),('covid-positive-pcr-test',1,'4J3R600',NULL,'SARS-CoV-2 RNA pos lim detect'),('covid-positive-pcr-test',1,'A7952',NULL,'COVID-19 confirmed by laboratory test'),('covid-positive-pcr-test',1,'A795200',NULL,'COVID-19 confirmed by laboratory test'),('covid-positive-pcr-test',1,'43hF.',NULL,'Detection of SARS-CoV-2 by PCR'),('covid-positive-pcr-test',1,'43hF.00',NULL,'Detection of SARS-CoV-2 by PCR'),('covid-positive-pcr-test',1,'43jS.',NULL,'Coronavirus nucleic acid detection'),('covid-positive-pcr-test',1,'43jS.00',NULL,'Coronavirus nucleic acid detection'),('covid-positive-pcr-test',1,'43jS0',NULL,'Coronavirus nucleic acid detection assay'),('covid-positive-pcr-test',1,'43jS000',NULL,'Coronavirus nucleic acid detection assay'),('covid-positive-pcr-test',1,'43jS1',NULL,'Coronavirus ribonucleic acid detection assay'),('covid-positive-pcr-test',1,'43jS100',NULL,'Coronavirus ribonucleic acid detection assay'); +INSERT INTO #codesreadv2 +VALUES ('covid-positive-test-other',1,'4J3R1',NULL,'2019-nCoV (novel coronavirus) detected'),('covid-positive-test-other',1,'4J3R100',NULL,'2019-nCoV (novel coronavirus) detected') + +INSERT INTO #AllCodes +SELECT [concept], [version], [code], [description] from #codesreadv2; + +IF OBJECT_ID('tempdb..#codesctv3') IS NOT NULL DROP TABLE #codesctv3; +CREATE TABLE #codesctv3 ( + [concept] [varchar](255) NOT NULL, + [version] INT NOT NULL, + [code] [varchar](20) COLLATE Latin1_General_CS_AS NOT NULL, + [term] [varchar](20) COLLATE Latin1_General_CS_AS NULL, + [description] [varchar](255) NULL +) ON [PRIMARY]; + +INSERT INTO #codesctv3 +VALUES ('covid-positive-antigen-test',1,'Y269d',NULL,'SARS-CoV-2 (severe acute respiratory syndrome coronavirus 2) antigen detection result positive'),('covid-positive-antigen-test',1,'43kB1',NULL,'SARS-CoV-2 antigen positive'); +INSERT INTO #codesctv3 +VALUES ('covid-positive-pcr-test',1,'4J3R6',NULL,'SARS-CoV-2 RNA pos lim detect'),('covid-positive-pcr-test',1,'Y240b',NULL,'Severe acute respiratory syndrome coronavirus 2 qualitative existence in specimen (observable entity)'),('covid-positive-pcr-test',1,'Y2a3b',NULL,'SARS-CoV-2 (severe acute respiratory syndrome coronavirus 2) RNA (ribonucleic acid) detection result positive'),('covid-positive-pcr-test',1,'A7952',NULL,'COVID-19 confirmed by laboratory test'),('covid-positive-pcr-test',1,'Y228d',NULL,'Coronavirus disease 19 caused by severe acute respiratory syndrome coronavirus 2 confirmed by laboratory test (situation)'),('covid-positive-pcr-test',1,'Y210e',NULL,'Detection of 2019-nCoV (novel coronavirus) using polymerase chain reaction technique'),('covid-positive-pcr-test',1,'43hF.',NULL,'Detection of SARS-CoV-2 by PCR'),('covid-positive-pcr-test',1,'Y2a3d',NULL,'SARS-CoV-2 (severe acute respiratory syndrome coronavirus 2) RNA (ribonucleic acid) detection result positive at the limit of detection'),('covid-positive-pcr-test',1,'Xab7a',NULL,'Coronavirus nucleic acid detection assay'),('covid-positive-pcr-test',1,'Xaboe',NULL,'Coronavirus ribonucleic acid detection assay'),('covid-positive-pcr-test',1,'XaLTE',NULL,'Coronavirus nucleic acid detection'); +INSERT INTO #codesctv3 +VALUES ('covid-positive-test-other',1,'4J3R1',NULL,'2019-nCoV (novel coronavirus) detected'),('covid-positive-test-other',1,'Y20d1',NULL,'Confirmed 2019-nCov (Wuhan) infection'),('covid-positive-test-other',1,'Y23f7',NULL,'SARS-CoV-2 (severe acute respiratory syndrome coronavirus 2) detection result positive') + +INSERT INTO #AllCodes +SELECT [concept], [version], [code], [description] from #codesctv3; + +IF OBJECT_ID('tempdb..#codessnomed') IS NOT NULL DROP TABLE #codessnomed; +CREATE TABLE #codessnomed ( + [concept] [varchar](255) NOT NULL, + [version] INT NOT NULL, + [code] [varchar](20) COLLATE Latin1_General_CS_AS NOT NULL, + [term] [varchar](20) COLLATE Latin1_General_CS_AS NULL, + [description] [varchar](255) NULL +) ON [PRIMARY]; + +INSERT INTO #codessnomed +VALUES ('covid-positive-antigen-test',1,'1322781000000102',NULL,'Severe acute respiratory syndrome coronavirus 2 antigen detection result positive (finding)'),('covid-positive-antigen-test',1,'871553007',NULL,'Detection of Severe acute respiratory syndrome coronavirus 2 antigen (observable entity)'),('covid-positive-antigen-test',1,'1240391000000107',NULL,'Antigen of severe acute respiratory syndrome coronavirus 2'),('covid-positive-antigen-test',1,'51631000237101',NULL,'Qualitative result of severe acute respiratory syndrome coronavirus 2 antigen in serum (observable entity)'); +INSERT INTO #codessnomed +VALUES ('covid-positive-pcr-test',1,'1240511000000106',NULL,'Detection of 2019 novel coronavirus using polymerase chain reaction technique (procedure)'),('covid-positive-pcr-test',1,'1300721000000109',NULL,'Coronavirus disease 19 caused by severe acute respiratory syndrome coronavirus 2 confirmed by laboratory test (situation)'),('covid-positive-pcr-test',1,'871560001',NULL,'Detection of ribonucleic acid of severe acute respiratory syndrome coronavirus 2 using polymerase chain reaction (observable entity)'),('covid-positive-pcr-test',1,'1269559008',NULL,'Detection of ribonucleic acid of severe acute respiratory syndrome coronavirus 2 using microchip real time polymerase chain reaction (observable entity)'),('covid-positive-pcr-test',1,'204351000000100',NULL,'Coronavirus nucleic acid detection (procedure)'),('covid-positive-pcr-test',1,'906711000000107',NULL,'Coronavirus nucleic acid detection assay (procedure)'),('covid-positive-pcr-test',1,'933791000000101',NULL,'Coronavirus ribonucleic acid detection assay (procedure)'),('covid-positive-pcr-test',1,'817111000000102',NULL,'Measurement of coronavirus ribonucleic acid by nucleic acid amplification test (procedure)'),('covid-positive-pcr-test',1,'1029481000000103',NULL,'Coronavirus nucleic acid detection assay (observable entity)'),('covid-positive-pcr-test',1,'1008541000000105',NULL,'Coronavirus ribonucleic acid detection assay (observable entity)'),('covid-positive-pcr-test',1,'1321301000000101',NULL,'Severe acute respiratory syndrome coronavirus 2 ribonucleic acid qualitative existence in specimen (observable entity)'),('covid-positive-pcr-test',1,'1324601000000106',NULL,'Severe acute respiratory syndrome coronavirus 2 ribonucleic acid detected (finding)'),('covid-positive-pcr-test',1,'1324881000000100',NULL,'Severe acute respiratory syndrome coronavirus 2 ribonucleic acid detection result positive at the limit of detection (finding)'); +INSERT INTO #codessnomed +VALUES ('covid-positive-test-other',1,'1240581000000104',NULL,'Severe acute respiratory syndrome coronavirus 2 detected (finding)'),('covid-positive-test-other',1,'1321301000000101',NULL,'Severe acute respiratory syndrome coronavirus 2 qualitative existence in specimen (observable entity)'),('covid-positive-test-other',1,'1324601000000106',NULL,'Severe acute respiratory syndrome coronavirus 2 ribonucleic acid detected (finding)'),('covid-positive-test-other',1,'1324881000000100',NULL,'Severe acute respiratory syndrome coronavirus 2 ribonucleic acid detection result positive at the limit of detection (finding)'),('covid-positive-test-other',1,'871562009',NULL,'Detection of Severe acute respiratory syndrome coronavirus 2 (observable entity)'),('covid-positive-test-other',1,'1300731000000106',NULL,'Coronavirus disease 19 caused by severe acute respiratory syndrome coronavirus 2 confirmed using clinical diagnostic criteria (situation)'),('covid-positive-test-other',1,'1240751000000100 ',NULL,'Coronavirus disease 19 caused by severe acute respiratory syndrome coronavirus 2'),('covid-positive-test-other',1,'189486241000119100 ',NULL,'Asymptomatic Severe acute respiratory syndrome coronavirus 2 infection'),('covid-positive-test-other',1,'871555000',NULL,'Detection of ribonucleic acid of severe acute respiratory syndrome coronavirus 2 (observable entity)'),('covid-positive-test-other',1,'50321000237103',NULL,'Qualitative result of severe acute respiratory syndrome coronavirus 2 ribonucleic acid nucleic acid amplification (observable entity)'),('covid-positive-test-other',1,'871560001',NULL,'Detection of ribonucleic acid of severe acute respiratory syndrome coronavirus 2 using polymerase chain reaction (observable entity)'),('covid-positive-test-other',1,'1269559008',NULL,'Detection of ribonucleic acid of severe acute respiratory syndrome coronavirus 2 using microchip real time polymerase chain reaction (observable entity)'),('covid-positive-test-other',1,'871559006',NULL,'Detection of ribonucleic acid of severe acute respiratory syndrome coronavirus 2 in bronchoalveolar lavage fluid (observable entity)'),('covid-positive-test-other',1,'871558003',NULL,'Detection of ribonucleic acid of severe acute respiratory syndrome coronavirus 2 in sputum (observable entity)'),('covid-positive-test-other',1,'871557008',NULL,'Detection of ribonucleic acid of severe acute respiratory syndrome coronavirus 2 in oropharyngeal swab (observable entity)'),('covid-positive-test-other',1,'871556004',NULL,'Detection of ribonucleic acid of severe acute respiratory syndrome coronavirus 2 in nasopharyngeal swab (observable entity)'),('covid-positive-test-other',1,'871553007',NULL,'Detection of severe acute respiratory syndrome coronavirus 2 antigen (observable entity)'),('covid-positive-test-other',1,'51631000237101',NULL,'Qualitative result of severe acute respiratory syndrome coronavirus 2 antigen in serum (observable entity)'),('covid-positive-test-other',1,'840539006',NULL,'Disease caused by Severe acute respiratory syndrome coronavirus 2 (disorder)'),('covid-positive-test-other',1,'840544004',NULL,'Suspected disease caused by 2019 novel coronavirus (situation)'),('covid-positive-test-other',1,'1325171000000109',NULL,'Acute disease caused by severe acute respiratory syndrome coronavirus 2 infection (disorder)'),('covid-positive-test-other',1,'1240761000000102',NULL,'Suspected disease caused by 2019 novel coronavirus (situation)'),('covid-positive-test-other',1,'1300681000000102',NULL,'Assessment using coronavirus disease 19 severity scale (procedure)'),('covid-positive-test-other',1,'1325181000000106',NULL,'Ongoing symptomatic disease caused by severe acute respiratory syndrome coronavirus 2 (disorder)') + +INSERT INTO #AllCodes +SELECT [concept], [version], [code], [description] from #codessnomed; + +IF OBJECT_ID('tempdb..#codesemis') IS NOT NULL DROP TABLE #codesemis; +CREATE TABLE #codesemis ( + [concept] [varchar](255) NOT NULL, + [version] INT NOT NULL, + [code] [varchar](20) COLLATE Latin1_General_CS_AS NOT NULL, + [term] [varchar](20) COLLATE Latin1_General_CS_AS NULL, + [description] [varchar](255) NULL +) ON [PRIMARY]; + +INSERT INTO #codesemis +VALUES ('covid-positive-antigen-test',1,'^ESCT1305304',NULL,'SARS-CoV-2 (severe acute respiratory syndrome coronavirus 2) antigen detection result positive'),('covid-positive-antigen-test',1,'^ESCT1348538',NULL,'Detection of SARS-CoV-2 (severe acute respiratory syndrome coronavirus 2) antigen'),('covid-positive-antigen-test',1,'^ESCT1305305',NULL,'2019-nCoV (novel coronavirus) antigen detection result positive'),('covid-positive-antigen-test',1,'^ESCT1348539',NULL,'Detection of Severe acute respiratory syndrome coronavirus 2 antigen'),('covid-positive-antigen-test',1,'^ESCT1348540',NULL,'Detection of 2019 novel coronavirus antigen'),('covid-positive-antigen-test',1,'^ESCT1348541',NULL,'Detection of 2019-nCoV (novel coronavirus) antigen'),('covid-positive-antigen-test',1,'^ESCT1299023',NULL,'Antigen of 2019-nCoV (novel coronavirus)'),('covid-positive-antigen-test',1,'^ESCT1299024',NULL,'Antigen of Wuhan 2019-nCoV (novel coronavirus)'),('covid-positive-antigen-test',1,'^ESCT1299025',NULL,'Antigen of 2019 novel coronavirus'),('covid-positive-antigen-test',1,'^ESCT1301213',NULL,'Antigen of SARS-CoV-2 (severe acute respiratory syndrome coronavirus 2)'); +INSERT INTO #codesemis +VALUES ('covid-positive-pcr-test',1,'^ESCT1305238',NULL,'SARS-CoV-2 (severe acute respiratory syndrome coronavirus 2) RNA (ribonucleic acid) qualitative existence in specimen'),('covid-positive-pcr-test',1,'^ESCT1348314',NULL,'SARS-CoV-2 (severe acute respiratory syndrome coronavirus 2) RNA (ribonucleic acid) detection result positive'),('covid-positive-pcr-test',1,'^ESCT1305235',NULL,'SARS-CoV-2 (severe acute respiratory syndrome coronavirus 2) RNA (ribonucleic acid) detection result positive'),('covid-positive-pcr-test',1,'^ESCT1300228',NULL,'COVID-19 confirmed by laboratory test GP COVID-19'),('covid-positive-pcr-test',1,'^ESCT1348316',NULL,'2019-nCoV (novel coronavirus) ribonucleic acid detected'),('covid-positive-pcr-test',1,'^ESCT1301223',NULL,'Detection of SARS-CoV-2 (severe acute respiratory syndrome coronavirus 2) using polymerase chain reaction technique'),('covid-positive-pcr-test',1,'^ESCT1348359',NULL,'SARS-CoV-2 (severe acute respiratory syndrome coronavirus 2) RNA (ribonucleic acid) detection result positive at the limit of detection'),('covid-positive-pcr-test',1,'^ESCT1299053',NULL,'Detection of 2019-nCoV (novel coronavirus) using polymerase chain reaction technique'),('covid-positive-pcr-test',1,'^ESCT1300228',NULL,'COVID-19 confirmed by laboratory test'),('covid-positive-pcr-test',1,'^ESCT1348359',NULL,'SARS-CoV-2 (severe acute respiratory syndrome coronavirus 2) RNA (ribonucleic acid) detection result positive at the limit of detection'),('covid-positive-pcr-test',1,'^ESCT1299054',NULL,'Detection of Wuhan 2019-nCoV (novel coronavirus) using polymerase chain reaction technique'),('covid-positive-pcr-test',1,'^ESCT1299055',NULL,'Detection of 2019 novel coronavirus using polymerase chain reaction technique'),('covid-positive-pcr-test',1,'^ESCT1348570',NULL,'Detection of RNA (ribonucleic acid) of SARS-CoV-2 (severe acute respiratory syndrome coronavirus 2) using polymerase chain reaction'),('covid-positive-pcr-test',1,'^ESCT1348571',NULL,'Detection of ribonucleic acid of COVID-19 using polymerase chain reaction'),('covid-positive-pcr-test',1,'^ESCT1348572',NULL,'Detection of ribonucleic acid of 2019 novel coronavirus using polymerase chain reaction'),('covid-positive-pcr-test',1,'^ESCT1348573',NULL,'Detection of ribonucleic acid of Severe acute respiratory syndrome coronavirus 2 using polymerase chain reaction'),('covid-positive-pcr-test',1,'^ESCT1348574',NULL,'Detection of ribonucleic acid of 2019-nCoV (novel coronavirus) using polymerase chain reaction'),('covid-positive-pcr-test',1,'^ESCTCO806483',NULL,'Coronavirus nucleic acid detection'),('covid-positive-pcr-test',1,'^ESCTCO832162',NULL,'Coronavirus nucleic acid detection assay'),('covid-positive-pcr-test',1,'^ESCTCO833909',NULL,'Coronavirus RNA (ribonucleic acid) detection assay'),('covid-positive-pcr-test',1,'^ESCT1348315',NULL,'Severe acute respiratory syndrome coronavirus 2 ribonucleic acid detected'); +INSERT INTO #codesemis +VALUES ('covid-positive-test-other',1,'^ESCT1303928',NULL,'SARS-CoV-2 (severe acute respiratory syndrome coronavirus 2) detection result positive'),('covid-positive-test-other',1,'^ESCT1299074',NULL,'2019-nCoV (novel coronavirus) detected'),('covid-positive-test-other',1,'^ESCT1301230',NULL,'SARS-CoV-2 (severe acute respiratory syndrome coronavirus 2) detected'),('covid-positive-test-other',1,'EMISNQCO303',NULL,'Confirmed 2019-nCoV (Wuhan) infectio'),('covid-positive-test-other',1,'^ESCT1299075',NULL,'Wuhan 2019-nCoV (novel coronavirus) detected'),('covid-positive-test-other',1,'^ESCT1300229',NULL,'COVID-19 confirmed using clinical diagnostic criteria'),('covid-positive-test-other',1,'^ESCT1348575',NULL,'Detection of SARS-CoV-2 (severe acute respiratory syndrome coronavirus 2)'),('covid-positive-test-other',1,'^ESCT1299074',NULL,'2019-nCoV (novel coronavirus) detected'),('covid-positive-test-other',1,'^ESCT1300229',NULL,'COVID-19 confirmed using clinical diagnostic criteria'),('covid-positive-test-other',1,'EMISNQCO303',NULL,'Confirmed 2019-nCoV (novel coronavirus) infection'),('covid-positive-test-other',1,'EMISNQCO303',NULL,'Confirmed 2019-nCoV (novel coronavirus) infection'),('covid-positive-test-other',1,'^ESCT1348575',NULL,'Detection of SARS-CoV-2 (severe acute respiratory syndrome coronavirus 2)'),('covid-positive-test-other',1,'^ESCT1299076',NULL,'2019 novel coronavirus detected'),('covid-positive-test-other',1,'^ESCT1303246',NULL,'SARS-CoV-2 (severe acute respiratory syndrome coronavirus 2) qualitative existence in specimen'),('covid-positive-test-other',1,'^ESCT1303247',NULL,'2019-nCoV (novel coronavirus) qualitative existence in specimen'),('covid-positive-test-other',1,'^ESCT1348315',NULL,'Severe acute respiratory syndrome coronavirus 2 ribonucleic acid detected'),('covid-positive-test-other',1,'^ESCT1348360',NULL,'2019-nCoV (novel coronavirus) detection result positive at the limit of detection'),('covid-positive-test-other',1,'^ESCT1348394',NULL,'Severe acute respiratory syndrome coronavirus 2 detected'),('covid-positive-test-other',1,'^ESCT1348395',NULL,'COVID-19 detected'),('covid-positive-test-other',1,'^ESCT1300230',NULL,'Probable COVID-19 confirmed using clinical diagnostic criteria'),('covid-positive-test-other',1,'^ESCT1300231',NULL,'COVID-19 confirmed clinically'),('covid-positive-test-other',1,'^ESCT1305235',NULL,'SARS-CoV-2 (severe acute respiratory syndrome coronavirus 2) RNA (ribonucleic acid) detection result positive'),('covid-positive-test-other',1,'^ESCT1305238',NULL,'SARS-CoV-2 (severe acute respiratory syndrome coronavirus 2) RNA (ribonucleic acid) qualitative existence in specimen'),('covid-positive-test-other',1,'^ESCT1348314',NULL,'SARS-CoV-2 (severe acute respiratory syndrome coronavirus 2) RNA (ribonucleic acid) detection result positive'),('covid-positive-test-other',1,'^ESCT1348316',NULL,'2019-nCoV (novel coronavirus) ribonucleic acid detected'),('covid-positive-test-other',1,'^ESCT1348359',NULL,'SARS-CoV-2 (severe acute respiratory syndrome coronavirus 2) RNA (ribonucleic acid) detection result positive at the limit of detection'),('covid-positive-test-other',1,'^ESCT1348576',NULL,'Detection of Severe acute respiratory syndrome coronavirus 2'),('covid-positive-test-other',1,'^ESCT1348577',NULL,'Detection of COVID-19'),('covid-positive-test-other',1,'^ESCT1348578',NULL,'2019 novel coronavirus assay'),('covid-positive-test-other',1,'^ESCT1348579',NULL,'Detection of 2019-nCoV (novel coronavirus)'),('covid-positive-test-other',1,'^ESCT1299074',NULL,'2019-nCoV (novel coronavirus) detected'),('covid-positive-test-other',1,'^ESCT1299075',NULL,'Wuhan 2019-nCoV (novel coronavirus) detected'),('covid-positive-test-other',1,'^ESCT1299076',NULL,'2019 novel coronavirus detected'),('covid-positive-test-other',1,'^ESCT1301230',NULL,'SARS-CoV-2 (severe acute respiratory syndrome coronavirus 2) detected'),('covid-positive-test-other',1,'^ESCT1303246',NULL,'SARS-CoV-2 (severe acute respiratory syndrome coronavirus 2) qualitative existence in specimen'),('covid-positive-test-other',1,'^ESCT1303247',NULL,'2019-nCoV (novel coronavirus) qualitative existence in specimen'),('covid-positive-test-other',1,'^ESCT1303928',NULL,'SARS-CoV-2 (severe acute respiratory syndrome coronavirus 2) detection result positive'),('covid-positive-test-other',1,'^ESCT1348360',NULL,'2019-nCoV (novel coronavirus) detection result positive at the limit of detection'),('covid-positive-test-other',1,'^ESCT1348394',NULL,'Severe acute respiratory syndrome coronavirus 2 detected'),('covid-positive-test-other',1,'^ESCT1348395',NULL,'COVID-19 detected'),('covid-positive-test-other',1,'^ESCT1299113',NULL,'Disease caused by 2019-nCoV (novel coronavirus)'),('covid-positive-test-other',1,'^ESCT1299114',NULL,'Disease caused by Wuhan 2019-nCoV (novel coronavirus)'),('covid-positive-test-other',1,'^ESCT1299115',NULL,'Disease caused by 2019 novel coronavirus'),('covid-positive-test-other',1,'^ESCT1301243',NULL,'COVID-19'),('covid-positive-test-other',1,'^ESCT1301244',NULL,'COVID-19 caused by SARS-CoV-2 (severe acute respiratory syndrome coronavirus 2)'),('covid-positive-test-other',1,'^ESCT1348538',NULL,'Detection of SARS-CoV-2 (severe acute respiratory syndrome coronavirus 2) antigen'),('covid-positive-test-other',1,'^ESCT1348539',NULL,'Detection of Severe acute respiratory syndrome coronavirus 2 antigen'),('covid-positive-test-other',1,'^ESCT1348540',NULL,'Detection of 2019 novel coronavirus antigen'),('covid-positive-test-other',1,'^ESCT1348541',NULL,'Detection of 2019-nCoV (novel coronavirus) antigen'),('covid-positive-test-other',1,'^ESCT1348542',NULL,'Detection of ribonucleic acid of Severe acute respiratory syndrome coronavirus 2'),('covid-positive-test-other',1,'^ESCT1348543',NULL,'Detection of ribonucleic acid of COVID-19'),('covid-positive-test-other',1,'^ESCT1348544',NULL,'2019 novel coronavirus ribonucleic acid assay'),('covid-positive-test-other',1,'^ESCT1348545',NULL,'COVID-19 ribonucleic acid assay'),('covid-positive-test-other',1,'^ESCT1348546',NULL,'Detection of RNA (ribonucleic acid) of SARS-CoV-2 (severe acute respiratory syndrome coronavirus 2) in nasopharyngeal swab'),('covid-positive-test-other',1,'^ESCT1348547',NULL,'Detection of ribonucleic acid of 2019 novel coronavirus in nasopharyngeal swab'),('covid-positive-test-other',1,'^ESCT1348548',NULL,'Nasopharyngeal swab COVID-19 ribonucleic acid assay'),('covid-positive-test-other',1,'^ESCT1348549',NULL,'Detection of ribonucleic acid of Severe acute respiratory syndrome coronavirus 2 in nasopharyngeal swab'),('covid-positive-test-other',1,'^ESCT1348550',NULL,'Detection of ribonucleic acid of COVID-19 in nasopharyngeal swab'),('covid-positive-test-other',1,'^ESCT1348551',NULL,'Detection of ribonucleic acid of 2019-nCoV (novel coronavirus) in nasopharyngeal swab'),('covid-positive-test-other',1,'^ESCT1348552',NULL,'Detection of RNA (ribonucleic acid) of SARS-CoV-2 (severe acute respiratory syndrome coronavirus 2) in oropharyngeal swab'),('covid-positive-test-other',1,'^ESCT1348553',NULL,'Detection of ribonucleic acid of 2019 novel coronavirus in oropharyngeal swab'),('covid-positive-test-other',1,'^ESCT1348554',NULL,'Oropharyngeal swab COVID-19 ribonucleic acid assay'),('covid-positive-test-other',1,'^ESCT1348555',NULL,'Detection of ribonucleic acid of COVID-19 in oropharyngeal swab'),('covid-positive-test-other',1,'^ESCT1348556',NULL,'Detection of ribonucleic acid of Severe acute respiratory syndrome coronavirus 2 in oropharyngeal swab'),('covid-positive-test-other',1,'^ESCT1348557',NULL,'Detection of ribonucleic acid of 2019-nCoV (novel coronavirus) in oropharyngeal swab'),('covid-positive-test-other',1,'^ESCT1348558',NULL,'Detection of RNA (ribonucleic acid) of SARS-CoV-2 (severe acute respiratory syndrome coronavirus 2) in sputum'),('covid-positive-test-other',1,'^ESCT1348559',NULL,'Detection of ribonucleic acid of Severe acute respiratory syndrome coronavirus 2 in sputum'),('covid-positive-test-other',1,'^ESCT1348560',NULL,'Sputum COVID-19 ribonucleic acid assay'),('covid-positive-test-other',1,'^ESCT1348561',NULL,'Detection of ribonucleic acid of COVID-19 in sputum'),('covid-positive-test-other',1,'^ESCT1348562',NULL,'Detection of ribonucleic acid of 2019 novel coronavirus in sputum'),('covid-positive-test-other',1,'^ESCT1348563',NULL,'Detection of ribonucleic acid of 2019-nCoV (novel coronavirus) in sputum'),('covid-positive-test-other',1,'^ESCT1348564',NULL,'Detection of RNA (ribonucleic acid) of SARS-CoV-2 (severe acute respiratory syndrome coronavirus 2) in bronchoalveolar lavage fluid'),('covid-positive-test-other',1,'^ESCT1348565',NULL,'Detection of ribonucleic acid of COVID-19 in bronchoalveolar lavage fluid'),('covid-positive-test-other',1,'^ESCT1348566',NULL,'Bronchoalveolar lavage fluid COVID-19 ribonucleic acid assay'),('covid-positive-test-other',1,'^ESCT1348567',NULL,'Detection of ribonucleic acid of Severe acute respiratory syndrome coronavirus 2 in bronchoalveolar lavage fluid'),('covid-positive-test-other',1,'^ESCT1348568',NULL,'Detection of ribonucleic acid of 2019 novel coronavirus in bronchoalveolar lavage fluid'),('covid-positive-test-other',1,'^ESCT1348569',NULL,'Detection of ribonucleic acid of 2019-nCoV (novel coronavirus) in bronchoalveolar lavage fluid'),('covid-positive-test-other',1,'^ESCT1348570',NULL,'Detection of RNA (ribonucleic acid) of SARS-CoV-2 (severe acute respiratory syndrome coronavirus 2) using polymerase chain reaction'),('covid-positive-test-other',1,'^ESCT1348571',NULL,'Detection of ribonucleic acid of COVID-19 using polymerase chain reaction'),('covid-positive-test-other',1,'^ESCT1348572',NULL,'Detection of ribonucleic acid of 2019 novel coronavirus using polymerase chain reaction'),('covid-positive-test-other',1,'^ESCT1348573',NULL,'Detection of ribonucleic acid of Severe acute respiratory syndrome coronavirus 2 using polymerase chain reaction'), +('covid-positive-test-other',1,'^ESCT1348574',NULL,'Detection of ribonucleic acid of 2019-nCoV (novel coronavirus) using polymerase chain reaction'),('covid-positive-test-other',1,'^ESCT1348656',NULL,'Asymptomatic SARS-CoV-2 (severe acute respiratory syndrome coronavirus 2) infection'),('covid-positive-test-other',1,'^ESCT1348657',NULL,'Asymptomatic COVID-19'),('covid-positive-test-other',1,'^ESCT1348658',NULL,'Asymptomatic 2019 novel coronavirus'),('covid-positive-test-other',1,'^ESCT1348659',NULL,'Asymptomatic Severe acute respiratory syndrome coronavirus 2 infection'),('covid-positive-test-other',1,'^ESCT1348660',NULL,'Asymptomatic 2019-nCoV (novel coronavirus) infection'),('covid-positive-test-other',1,'^ESCT1421535',NULL,'Detection of ribonucleic acid of SARS-CoV-2 in bronchoalveolar lavage fluid') + +INSERT INTO #AllCodes +SELECT [concept], [version], [code], [description] from #codesemis; + + +IF OBJECT_ID('tempdb..#TempRefCodes') IS NOT NULL DROP TABLE #TempRefCodes; +CREATE TABLE #TempRefCodes (FK_Reference_Coding_ID BIGINT NOT NULL, concept VARCHAR(255) NOT NULL, version INT NOT NULL, [description] VARCHAR(255)); + +-- Read v2 codes +INSERT INTO #TempRefCodes +SELECT PK_Reference_Coding_ID, dcr.concept, dcr.[version], dcr.[description] +FROM [SharedCare].[Reference_Coding] rc +INNER JOIN #codesreadv2 dcr on dcr.code = rc.MainCode +WHERE CodingType='ReadCodeV2' +AND (dcr.term IS NULL OR dcr.term = rc.Term) +and PK_Reference_Coding_ID != -1; + +-- CTV3 codes +INSERT INTO #TempRefCodes +SELECT PK_Reference_Coding_ID, dcc.concept, dcc.[version], dcc.[description] +FROM [SharedCare].[Reference_Coding] rc +INNER JOIN #codesctv3 dcc on dcc.code = rc.MainCode +WHERE CodingType='CTV3' +and PK_Reference_Coding_ID != -1; + +-- EMIS codes with a FK Reference Coding ID +INSERT INTO #TempRefCodes +SELECT FK_Reference_Coding_ID, ce.concept, ce.[version], ce.[description] +FROM [SharedCare].[Reference_Local_Code] rlc +INNER JOIN #codesemis ce on ce.code = rlc.LocalCode +WHERE FK_Reference_Coding_ID != -1; + +IF OBJECT_ID('tempdb..#TempSNOMEDRefCodes') IS NOT NULL DROP TABLE #TempSNOMEDRefCodes; +CREATE TABLE #TempSNOMEDRefCodes (FK_Reference_SnomedCT_ID BIGINT NOT NULL, concept VARCHAR(255) NOT NULL, [version] INT NOT NULL, [description] VARCHAR(255)); + +-- SNOMED codes +INSERT INTO #TempSNOMEDRefCodes +SELECT PK_Reference_SnomedCT_ID, dcs.concept, dcs.[version], dcs.[description] +FROM SharedCare.Reference_SnomedCT rs +INNER JOIN #codessnomed dcs on dcs.code = rs.ConceptID; + +-- EMIS codes with a FK SNOMED ID but without a FK Reference Coding ID +INSERT INTO #TempSNOMEDRefCodes +SELECT FK_Reference_SnomedCT_ID, ce.concept, ce.[version], ce.[description] +FROM [SharedCare].[Reference_Local_Code] rlc +INNER JOIN #codesemis ce on ce.code = rlc.LocalCode +WHERE FK_Reference_Coding_ID = -1 +AND FK_Reference_SnomedCT_ID != -1; + +-- De-duped tables +IF OBJECT_ID('tempdb..#CodeSets') IS NOT NULL DROP TABLE #CodeSets; +CREATE TABLE #CodeSets (FK_Reference_Coding_ID BIGINT NOT NULL, concept VARCHAR(255) NOT NULL, [description] VARCHAR(255)); + +IF OBJECT_ID('tempdb..#SnomedSets') IS NOT NULL DROP TABLE #SnomedSets; +CREATE TABLE #SnomedSets (FK_Reference_SnomedCT_ID BIGINT NOT NULL, concept VARCHAR(255) NOT NULL, [description] VARCHAR(255)); + +IF OBJECT_ID('tempdb..#VersionedCodeSets') IS NOT NULL DROP TABLE #VersionedCodeSets; +CREATE TABLE #VersionedCodeSets (FK_Reference_Coding_ID BIGINT NOT NULL, Concept VARCHAR(255), [Version] INT, [description] VARCHAR(255)); + +IF OBJECT_ID('tempdb..#VersionedSnomedSets') IS NOT NULL DROP TABLE #VersionedSnomedSets; +CREATE TABLE #VersionedSnomedSets (FK_Reference_SnomedCT_ID BIGINT NOT NULL, Concept VARCHAR(255), [Version] INT, [description] VARCHAR(255)); + +INSERT INTO #VersionedCodeSets +SELECT DISTINCT * FROM #TempRefCodes; + +INSERT INTO #VersionedSnomedSets +SELECT DISTINCT * FROM #TempSNOMEDRefCodes; + +INSERT INTO #CodeSets +SELECT FK_Reference_Coding_ID, c.concept, [description] +FROM #VersionedCodeSets c +INNER JOIN ( + SELECT concept, MAX(version) AS maxVersion FROM #VersionedCodeSets + GROUP BY concept) +sub ON sub.concept = c.concept AND c.version = sub.maxVersion; + +INSERT INTO #SnomedSets +SELECT FK_Reference_SnomedCT_ID, c.concept, [description] +FROM #VersionedSnomedSets c +INNER JOIN ( + SELECT concept, MAX(version) AS maxVersion FROM #VersionedSnomedSets + GROUP BY concept) +sub ON sub.concept = c.concept AND c.version = sub.maxVersion; + +--#endregion + +-- >>> Following code sets injected: covid-positive-antigen-test v1/covid-positive-pcr-test v1/covid-positive-test-other v1 + + +-- Set the temp end date until new legal basis - OLD +--DECLARE @TEMPWithCovidEndDate datetime; +--SET @TEMPWithCovidEndDate = '2022-06-01'; + +IF OBJECT_ID('tempdb..#CovidPatientsAllDiagnoses') IS NOT NULL DROP TABLE #CovidPatientsAllDiagnoses; +CREATE TABLE #CovidPatientsAllDiagnoses ( + FK_Patient_Link_ID BIGINT, + CovidPositiveDate DATE +); + +INSERT INTO #CovidPatientsAllDiagnoses +SELECT DISTINCT FK_Patient_Link_ID, CONVERT(DATE, [EventDate]) AS CovidPositiveDate +FROM [SharedCare].[COVID19] +WHERE ( + (GroupDescription = 'Confirmed' AND SubGroupDescription != 'Negative') OR + (GroupDescription = 'Tested' AND SubGroupDescription = 'Positive') +) +AND EventDate > '2020-01-01' +--AND EventDate <= @TEMPWithCovidEndDate +--AND EventDate <= GETDATE() +AND FK_Patient_Link_ID IN (SELECT FK_Patient_Link_ID FROM #Patients); + +-- We can rely on the GraphNet table for first diagnosis. +IF OBJECT_ID('tempdb..#CovidPatients') IS NOT NULL DROP TABLE #CovidPatients; +SELECT FK_Patient_Link_ID, MIN(CovidPositiveDate) AS FirstCovidPositiveDate INTO #CovidPatients +FROM #CovidPatientsAllDiagnoses +GROUP BY FK_Patient_Link_ID; + +-- Now let's get the dates of any positive test (i.e. not things like suspected, or historic) +IF OBJECT_ID('tempdb..#AllPositiveTestsTemp') IS NOT NULL DROP TABLE #AllPositiveTestsTemp; +CREATE TABLE #AllPositiveTestsTemp ( + FK_Patient_Link_ID BIGINT, + TestDate DATE +); + +INSERT INTO #AllPositiveTestsTemp +SELECT DISTINCT FK_Patient_Link_ID, CAST(EventDate AS DATE) AS TestDate +FROM SharedCare.GP_Events +WHERE SuppliedCode IN ( + select Code from #AllCodes + where Concept in ('covid-positive-antigen-test','covid-positive-pcr-test','covid-positive-test-other') + AND Version = 1 +) +--AND EventDate <= @TEMPWithCovidEndDate +AND FK_Patient_Link_ID IN (SELECT FK_Patient_Link_ID FROM #Patients); + +IF OBJECT_ID('tempdb..#CovidPatientsMultipleDiagnoses') IS NOT NULL DROP TABLE #CovidPatientsMultipleDiagnoses; +CREATE TABLE #CovidPatientsMultipleDiagnoses ( + FK_Patient_Link_ID BIGINT, + FirstCovidPositiveDate DATE, + SecondCovidPositiveDate DATE, + ThirdCovidPositiveDate DATE, + FourthCovidPositiveDate DATE, + FifthCovidPositiveDate DATE +); + +-- Populate first diagnosis +INSERT INTO #CovidPatientsMultipleDiagnoses (FK_Patient_Link_ID, FirstCovidPositiveDate) +SELECT FK_Patient_Link_ID, MIN(FirstCovidPositiveDate) FROM +( + SELECT * FROM #CovidPatients + UNION + SELECT * FROM #AllPositiveTestsTemp +) sub +GROUP BY FK_Patient_Link_ID; + +-- Now let's get second tests. +UPDATE t1 +SET t1.SecondCovidPositiveDate = NextTestDate +FROM #CovidPatientsMultipleDiagnoses AS t1 +INNER JOIN ( +SELECT cp.FK_Patient_Link_ID, MIN(apt.TestDate) AS NextTestDate FROM #CovidPatients cp +INNER JOIN #AllPositiveTestsTemp apt ON cp.FK_Patient_Link_ID = apt.FK_Patient_Link_ID AND apt.TestDate >= DATEADD(day, 90, FirstCovidPositiveDate) +GROUP BY cp.FK_Patient_Link_ID) AS sub ON sub.FK_Patient_Link_ID = t1.FK_Patient_Link_ID; + +-- Now let's get third tests. +UPDATE t1 +SET t1.ThirdCovidPositiveDate = NextTestDate +FROM #CovidPatientsMultipleDiagnoses AS t1 +INNER JOIN ( +SELECT cp.FK_Patient_Link_ID, MIN(apt.TestDate) AS NextTestDate FROM #CovidPatientsMultipleDiagnoses cp +INNER JOIN #AllPositiveTestsTemp apt ON cp.FK_Patient_Link_ID = apt.FK_Patient_Link_ID AND apt.TestDate >= DATEADD(day, 90, SecondCovidPositiveDate) +GROUP BY cp.FK_Patient_Link_ID) AS sub ON sub.FK_Patient_Link_ID = t1.FK_Patient_Link_ID; + +-- Now let's get fourth tests. +UPDATE t1 +SET t1.FourthCovidPositiveDate = NextTestDate +FROM #CovidPatientsMultipleDiagnoses AS t1 +INNER JOIN ( +SELECT cp.FK_Patient_Link_ID, MIN(apt.TestDate) AS NextTestDate FROM #CovidPatientsMultipleDiagnoses cp +INNER JOIN #AllPositiveTestsTemp apt ON cp.FK_Patient_Link_ID = apt.FK_Patient_Link_ID AND apt.TestDate >= DATEADD(day, 90, ThirdCovidPositiveDate) +GROUP BY cp.FK_Patient_Link_ID) AS sub ON sub.FK_Patient_Link_ID = t1.FK_Patient_Link_ID; + +-- Now let's get fifth tests. +UPDATE t1 +SET t1.FifthCovidPositiveDate = NextTestDate +FROM #CovidPatientsMultipleDiagnoses AS t1 +INNER JOIN ( +SELECT cp.FK_Patient_Link_ID, MIN(apt.TestDate) AS NextTestDate FROM #CovidPatientsMultipleDiagnoses cp +INNER JOIN #AllPositiveTestsTemp apt ON cp.FK_Patient_Link_ID = apt.FK_Patient_Link_ID AND apt.TestDate >= DATEADD(day, 90, FourthCovidPositiveDate) +GROUP BY cp.FK_Patient_Link_ID) AS sub ON sub.FK_Patient_Link_ID = t1.FK_Patient_Link_ID; +-- 1m38 --┌───────────────┐ --│ Year of birth │ --└───────────────┘ @@ -732,6 +1061,90 @@ LEFT OUTER JOIN #TownsendLookup t ON t.LSOA = lsoa.LSOA_Code; DROP TABLE #TownsendLookup; -- 8s +-- Whole population with COVID +IF OBJECT_ID('tempdb..#C19PopulationAll') IS NOT NULL DROP TABLE #C19PopulationAll; +SELECT + 'C19 Whole population' AS Descriptor, + COUNT(*) AS Total, + SUM(CASE WHEN sex.Sex = 'M' THEN 1 ELSE 0 END) As MaleTotal, + SUM(CASE WHEN sex.Sex = 'F' THEN 1 ELSE 0 END) As FemaleTotal +INTO #C19PopulationAll +FROM #Patients pat +LEFT OUTER JOIN #PatientSex sex ON sex.FK_Patient_Link_ID = pat.FK_Patient_Link_ID +LEFT OUTER JOIN SharedCare.Patient_Link pl ON pl.PK_Patient_Link_ID = pat.FK_Patient_Link_ID +INNER JOIN #CovidPatientsMultipleDiagnoses c ON c.FK_Patient_Link_ID = pat.FK_Patient_Link_ID +WHERE DeathDate IS NULL; + +-- 10 year age bands with COVID +IF OBJECT_ID('tempdb..#C19PopulationAgeBands') IS NOT NULL DROP TABLE #C19PopulationAgeBands; +SELECT + CASE + WHEN 2024 - yob.YearOfBirth <= 10 THEN 'C19 Age: 0-10' + WHEN 2024 - yob.YearOfBirth <= 20 THEN 'C19 Age: 11-20' + WHEN 2024 - yob.YearOfBirth <= 30 THEN 'C19 Age: 21-30' + WHEN 2024 - yob.YearOfBirth <= 40 THEN 'C19 Age: 31-40' + WHEN 2024 - yob.YearOfBirth <= 50 THEN 'C19 Age: 41-50' + WHEN 2024 - yob.YearOfBirth <= 60 THEN 'C19 Age: 51-60' + WHEN 2024 - yob.YearOfBirth <= 70 THEN 'C19 Age: 61-70' + WHEN 2024 - yob.YearOfBirth <= 80 THEN 'C19 Age: 71-80' + WHEN 2024 - yob.YearOfBirth <= 90 THEN 'C19 Age: 81-90' + ELSE 'C19 Age: 90+' + END AS Descriptor, + COUNT(*) As Total, + SUM(CASE WHEN sex.Sex = 'M' THEN 1 ELSE 0 END) As MaleTotal, + SUM(CASE WHEN sex.Sex = 'F' THEN 1 ELSE 0 END) As FemaleTotal +INTO #C19PopulationAgeBands +FROM #Patients pat +LEFT OUTER JOIN #PatientSex sex ON sex.FK_Patient_Link_ID = pat.FK_Patient_Link_ID +LEFT OUTER JOIN #PatientYearOfBirth yob ON yob.FK_Patient_Link_ID = pat.FK_Patient_Link_ID +LEFT OUTER JOIN SharedCare.Patient_Link pl ON pl.PK_Patient_Link_ID = pat.FK_Patient_Link_ID +INNER JOIN #CovidPatientsMultipleDiagnoses c ON c.FK_Patient_Link_ID = pat.FK_Patient_Link_ID +WHERE DeathDate IS NULL +GROUP BY CASE + WHEN 2024 - yob.YearOfBirth <= 10 THEN 'C19 Age: 0-10' + WHEN 2024 - yob.YearOfBirth <= 20 THEN 'C19 Age: 11-20' + WHEN 2024 - yob.YearOfBirth <= 30 THEN 'C19 Age: 21-30' + WHEN 2024 - yob.YearOfBirth <= 40 THEN 'C19 Age: 31-40' + WHEN 2024 - yob.YearOfBirth <= 50 THEN 'C19 Age: 41-50' + WHEN 2024 - yob.YearOfBirth <= 60 THEN 'C19 Age: 51-60' + WHEN 2024 - yob.YearOfBirth <= 70 THEN 'C19 Age: 61-70' + WHEN 2024 - yob.YearOfBirth <= 80 THEN 'C19 Age: 71-80' + WHEN 2024 - yob.YearOfBirth <= 90 THEN 'C19 Age: 81-90' + ELSE 'C19 Age: 90+' +END +ORDER BY Descriptor; + +-- Split by ethnicity with COVID +IF OBJECT_ID('tempdb..#C19PopulationEthnicity') IS NOT NULL DROP TABLE #C19PopulationEthnicity; +SELECT + CONCAT('C19 Ethnicity: ', ISNULL(EthnicMainGroup, 'zNULL')) AS Descriptor, + COUNT(*) As Total, + SUM(CASE WHEN sex.Sex = 'M' THEN 1 ELSE 0 END) As MaleTotal, + SUM(CASE WHEN sex.Sex = 'F' THEN 1 ELSE 0 END) As FemaleTotal +INTO #C19PopulationEthnicity +FROM #Patients pat +LEFT OUTER JOIN #PatientSex sex ON sex.FK_Patient_Link_ID = pat.FK_Patient_Link_ID +LEFT OUTER JOIN SharedCare.Patient_Link pl ON pl.PK_Patient_Link_ID = pat.FK_Patient_Link_ID +INNER JOIN #CovidPatientsMultipleDiagnoses c ON c.FK_Patient_Link_ID = pat.FK_Patient_Link_ID +WHERE DeathDate IS NULL +GROUP BY EthnicMainGroup +ORDER BY EthnicMainGroup; + +-- Split by townsend quintile with COVID +IF OBJECT_ID('tempdb..#C19PopulationTownsend') IS NOT NULL DROP TABLE #C19PopulationTownsend; +SELECT + CONCAT('C19 Townsend: ', ISNULL(TRY_CAST(TownsendQuintileHigherIsMoreDeprived as varchar), 'NULL')) AS Descriptor, + COUNT(*) As Total, + SUM(CASE WHEN sex.Sex = 'M' THEN 1 ELSE 0 END) As MaleTotal, + SUM(CASE WHEN sex.Sex = 'F' THEN 1 ELSE 0 END) As FemaleTotal +INTO #C19PopulationTownsend +FROM #Patients pat +LEFT OUTER JOIN #PatientSex sex ON sex.FK_Patient_Link_ID = pat.FK_Patient_Link_ID +LEFT OUTER JOIN #PatientTownsend town ON town.FK_Patient_Link_ID = pat.FK_Patient_Link_ID +LEFT OUTER JOIN SharedCare.Patient_Link pl ON pl.PK_Patient_Link_ID = pat.FK_Patient_Link_ID +INNER JOIN #CovidPatientsMultipleDiagnoses c ON c.FK_Patient_Link_ID = pat.FK_Patient_Link_ID +WHERE DeathDate IS NULL +GROUP BY TownsendQuintileHigherIsMoreDeprived; -- Whole population IF OBJECT_ID('tempdb..#PopulationAll') IS NOT NULL DROP TABLE #PopulationAll; @@ -821,4 +1234,12 @@ UNION SELECT * FROM #PopulationEthnicity UNION SELECT * FROM #PopulationTownsend +UNION +SELECT * FROM #C19PopulationAll +UNION +SELECT * FROM #C19PopulationAgeBands +UNION +SELECT * FROM #C19PopulationEthnicity +UNION +SELECT * FROM #C19PopulationTownsend ORDER BY Descriptor; \ No newline at end of file diff --git a/projects/066 - Heald (Long Covid)/template-sql/summary-stats.template.sql b/projects/066 - Heald (Long Covid)/template-sql/summary-stats.template.sql index 13540c95..f14cfb4d 100644 --- a/projects/066 - Heald (Long Covid)/template-sql/summary-stats.template.sql +++ b/projects/066 - Heald (Long Covid)/template-sql/summary-stats.template.sql @@ -18,6 +18,8 @@ WHERE FK_Reference_Tenancy_ID=2 AND GPPracticeCode NOT LIKE 'ZZZ%'; -- 21s +--> EXECUTE query-patients-with-covid.sql start-date:2020-01-01 all-patients:false gp-events-table:SharedCare.GP_Events +-- 1m38 --> EXECUTE query-patient-year-of-birth.sql -- 26s --> EXECUTE query-patient-sex.sql @@ -27,6 +29,90 @@ AND GPPracticeCode NOT LIKE 'ZZZ%'; --> EXECUTE query-patient-townsend.sql -- 8s +-- Whole population with COVID +IF OBJECT_ID('tempdb..#C19PopulationAll') IS NOT NULL DROP TABLE #C19PopulationAll; +SELECT + 'C19 Whole population' AS Descriptor, + COUNT(*) AS Total, + SUM(CASE WHEN sex.Sex = 'M' THEN 1 ELSE 0 END) As MaleTotal, + SUM(CASE WHEN sex.Sex = 'F' THEN 1 ELSE 0 END) As FemaleTotal +INTO #C19PopulationAll +FROM #Patients pat +LEFT OUTER JOIN #PatientSex sex ON sex.FK_Patient_Link_ID = pat.FK_Patient_Link_ID +LEFT OUTER JOIN SharedCare.Patient_Link pl ON pl.PK_Patient_Link_ID = pat.FK_Patient_Link_ID +INNER JOIN #CovidPatientsMultipleDiagnoses c ON c.FK_Patient_Link_ID = pat.FK_Patient_Link_ID +WHERE DeathDate IS NULL; + +-- 10 year age bands with COVID +IF OBJECT_ID('tempdb..#C19PopulationAgeBands') IS NOT NULL DROP TABLE #C19PopulationAgeBands; +SELECT + CASE + WHEN 2024 - yob.YearOfBirth <= 10 THEN 'C19 Age: 0-10' + WHEN 2024 - yob.YearOfBirth <= 20 THEN 'C19 Age: 11-20' + WHEN 2024 - yob.YearOfBirth <= 30 THEN 'C19 Age: 21-30' + WHEN 2024 - yob.YearOfBirth <= 40 THEN 'C19 Age: 31-40' + WHEN 2024 - yob.YearOfBirth <= 50 THEN 'C19 Age: 41-50' + WHEN 2024 - yob.YearOfBirth <= 60 THEN 'C19 Age: 51-60' + WHEN 2024 - yob.YearOfBirth <= 70 THEN 'C19 Age: 61-70' + WHEN 2024 - yob.YearOfBirth <= 80 THEN 'C19 Age: 71-80' + WHEN 2024 - yob.YearOfBirth <= 90 THEN 'C19 Age: 81-90' + ELSE 'C19 Age: 90+' + END AS Descriptor, + COUNT(*) As Total, + SUM(CASE WHEN sex.Sex = 'M' THEN 1 ELSE 0 END) As MaleTotal, + SUM(CASE WHEN sex.Sex = 'F' THEN 1 ELSE 0 END) As FemaleTotal +INTO #C19PopulationAgeBands +FROM #Patients pat +LEFT OUTER JOIN #PatientSex sex ON sex.FK_Patient_Link_ID = pat.FK_Patient_Link_ID +LEFT OUTER JOIN #PatientYearOfBirth yob ON yob.FK_Patient_Link_ID = pat.FK_Patient_Link_ID +LEFT OUTER JOIN SharedCare.Patient_Link pl ON pl.PK_Patient_Link_ID = pat.FK_Patient_Link_ID +INNER JOIN #CovidPatientsMultipleDiagnoses c ON c.FK_Patient_Link_ID = pat.FK_Patient_Link_ID +WHERE DeathDate IS NULL +GROUP BY CASE + WHEN 2024 - yob.YearOfBirth <= 10 THEN 'C19 Age: 0-10' + WHEN 2024 - yob.YearOfBirth <= 20 THEN 'C19 Age: 11-20' + WHEN 2024 - yob.YearOfBirth <= 30 THEN 'C19 Age: 21-30' + WHEN 2024 - yob.YearOfBirth <= 40 THEN 'C19 Age: 31-40' + WHEN 2024 - yob.YearOfBirth <= 50 THEN 'C19 Age: 41-50' + WHEN 2024 - yob.YearOfBirth <= 60 THEN 'C19 Age: 51-60' + WHEN 2024 - yob.YearOfBirth <= 70 THEN 'C19 Age: 61-70' + WHEN 2024 - yob.YearOfBirth <= 80 THEN 'C19 Age: 71-80' + WHEN 2024 - yob.YearOfBirth <= 90 THEN 'C19 Age: 81-90' + ELSE 'C19 Age: 90+' +END +ORDER BY Descriptor; + +-- Split by ethnicity with COVID +IF OBJECT_ID('tempdb..#C19PopulationEthnicity') IS NOT NULL DROP TABLE #C19PopulationEthnicity; +SELECT + CONCAT('C19 Ethnicity: ', ISNULL(EthnicMainGroup, 'zNULL')) AS Descriptor, + COUNT(*) As Total, + SUM(CASE WHEN sex.Sex = 'M' THEN 1 ELSE 0 END) As MaleTotal, + SUM(CASE WHEN sex.Sex = 'F' THEN 1 ELSE 0 END) As FemaleTotal +INTO #C19PopulationEthnicity +FROM #Patients pat +LEFT OUTER JOIN #PatientSex sex ON sex.FK_Patient_Link_ID = pat.FK_Patient_Link_ID +LEFT OUTER JOIN SharedCare.Patient_Link pl ON pl.PK_Patient_Link_ID = pat.FK_Patient_Link_ID +INNER JOIN #CovidPatientsMultipleDiagnoses c ON c.FK_Patient_Link_ID = pat.FK_Patient_Link_ID +WHERE DeathDate IS NULL +GROUP BY EthnicMainGroup +ORDER BY EthnicMainGroup; + +-- Split by townsend quintile with COVID +IF OBJECT_ID('tempdb..#C19PopulationTownsend') IS NOT NULL DROP TABLE #C19PopulationTownsend; +SELECT + CONCAT('C19 Townsend: ', ISNULL(TRY_CAST(TownsendQuintileHigherIsMoreDeprived as varchar), 'NULL')) AS Descriptor, + COUNT(*) As Total, + SUM(CASE WHEN sex.Sex = 'M' THEN 1 ELSE 0 END) As MaleTotal, + SUM(CASE WHEN sex.Sex = 'F' THEN 1 ELSE 0 END) As FemaleTotal +INTO #C19PopulationTownsend +FROM #Patients pat +LEFT OUTER JOIN #PatientSex sex ON sex.FK_Patient_Link_ID = pat.FK_Patient_Link_ID +LEFT OUTER JOIN #PatientTownsend town ON town.FK_Patient_Link_ID = pat.FK_Patient_Link_ID +LEFT OUTER JOIN SharedCare.Patient_Link pl ON pl.PK_Patient_Link_ID = pat.FK_Patient_Link_ID +INNER JOIN #CovidPatientsMultipleDiagnoses c ON c.FK_Patient_Link_ID = pat.FK_Patient_Link_ID +WHERE DeathDate IS NULL +GROUP BY TownsendQuintileHigherIsMoreDeprived; -- Whole population IF OBJECT_ID('tempdb..#PopulationAll') IS NOT NULL DROP TABLE #PopulationAll; @@ -116,4 +202,12 @@ UNION SELECT * FROM #PopulationEthnicity UNION SELECT * FROM #PopulationTownsend +UNION +SELECT * FROM #C19PopulationAll +UNION +SELECT * FROM #C19PopulationAgeBands +UNION +SELECT * FROM #C19PopulationEthnicity +UNION +SELECT * FROM #C19PopulationTownsend ORDER BY Descriptor; \ No newline at end of file