initial commit

This commit is contained in:
Andrew Charlwood
2026-05-12 16:40:03 +01:00
commit 647d1bfa7f
38 changed files with 2715 additions and 0 deletions
@@ -0,0 +1,20 @@
/*
Clinical coding cluster lookup
==============================
Purpose:
List SNOMED codes attached to maintained clinical coding clusters.
Use maintained clusters where possible instead of free-text matching on
SNOMED descriptions.
*/
SET CLUSTER_ID = 'DEPR_COD';
SELECT DISTINCT
ccs."Cluster_ID",
ccs."SNOMEDCode",
ccs."SNOMEDDescription"
FROM DATA_HUB.PHM."ClinicalCodingClusterSnomedCodes" ccs
WHERE ccs."Cluster_ID" = $CLUSTER_ID
ORDER BY ccs."SNOMEDDescription", ccs."SNOMEDCode";
@@ -0,0 +1,102 @@
/*
Monthly clinical event count by practice
========================================
Purpose:
Count distinct patients with a clinical code by month and practice, while
returning zero rows for months/practices with no events.
Good for:
- SMR-type activity counts.
- Condition or review-code monitoring.
- Building a complete month/practice grid for charts.
*/
SET START_MONTH = '2024-07-01';
SET END_MONTH = CURRENT_DATE();
SET CLUSTER_ID = 'REPLACE_WITH_CLUSTER_ID';
WITH clinical_codes AS (
-- Maintained cluster definitions avoid fragile free-text SNOMED searches.
SELECT DISTINCT "SNOMEDCode"
FROM DATA_HUB.PHM."ClinicalCodingClusterSnomedCodes"
WHERE "Cluster_ID" = $CLUSTER_ID
),
practices AS (
-- Default to active Norfolk and Suffolk parent GP practices.
SELECT DISTINCT
"OrganisationCode" AS "PracticeCode",
"OrganisationName" AS "PracticeName",
"PCNName",
"PlaceName",
"AllianceName"
FROM DATA_HUB.DWH."DimOrganisationAndSite"
WHERE "IsSiteNorfolkAndSuffolk" = 'Yes'
AND "OrganisationSubType" = 'GP Practice'
AND "IsSiteActive" = 'Yes'
AND "SiteCode" = "OrganisationCode"
),
base_population AS (
-- Keeps the denominator/cohort rule visible before events are counted.
SELECT
p."PracticeCode",
dp."PersonKey",
dp."PatientPseudonym"
FROM DATA_HUB.DWH."DimPerson" dp
INNER JOIN practices p
ON dp."CurrentGeneralPractice" = p."PracticeCode"
WHERE dp."RecordStatus" = 'Active'
AND dp."PersonStatus" = 'Known'
AND dp."YearMonthDeath" IS NULL
),
month_numbers AS (
-- Generates up to 120 monthly rows; extend if a longer history is needed.
SELECT ROW_NUMBER() OVER (ORDER BY SEQ4()) - 1 AS n
FROM TABLE(GENERATOR(ROWCOUNT => 120))
),
months AS (
SELECT DATE_TRUNC('MONTH', DATEADD(MONTH, n, $START_MONTH::DATE))::DATE AS "MonthStartDate"
FROM month_numbers
WHERE DATEADD(MONTH, n, $START_MONTH::DATE) <= $END_MONTH::DATE
),
practice_month_grid AS (
SELECT
p."PracticeCode",
p."PracticeName",
p."PCNName",
p."PlaceName",
p."AllianceName",
m."MonthStartDate"
FROM practices p
CROSS JOIN months m
),
events AS (
SELECT
bp."PracticeCode",
DATE_TRUNC('MONTH', cc."EventDateTime")::DATE AS "MonthStartDate",
COUNT(DISTINCT bp."PersonKey") AS "PatientsWithEvent"
FROM DATA_HUB.PHM."PrimaryCareClinicalCoding" cc
INNER JOIN clinical_codes c
ON cc."SNOMEDCode" = c."SNOMEDCode"
INNER JOIN base_population bp
ON cc."PatientPseudonym" = bp."PatientPseudonym"
WHERE cc."EventDateTime"::DATE BETWEEN $START_MONTH::DATE AND $END_MONTH::DATE
GROUP BY
bp."PracticeCode",
DATE_TRUNC('MONTH', cc."EventDateTime")::DATE
)
SELECT
pmg."PracticeCode",
pmg."PracticeName",
pmg."PCNName",
pmg."PlaceName",
pmg."AllianceName",
pmg."MonthStartDate",
COALESCE(e."PatientsWithEvent", 0) AS "PatientsWithEvent"
FROM practice_month_grid pmg
LEFT JOIN events e
ON pmg."PracticeCode" = e."PracticeCode"
AND pmg."MonthStartDate" = e."MonthStartDate"
ORDER BY
pmg."PracticeName",
pmg."MonthStartDate";
@@ -0,0 +1,114 @@
/*
Cohort from prescribing plus clinical coding
============================================
Purpose:
Build a patient cohort where a medicine exposure is combined with a clinical
coding condition, for example patients prescribed a medicine who also have
a relevant diagnosis code in a lookback window.
Replace:
- BNF_PREFIX or use a VTM/VMP product CTE.
- CLUSTER_ID with the maintained cluster needed for the clinical condition.
*/
SET PRESCRIBING_START_DATE = '2025-04-01';
SET PRESCRIBING_END_DATE = '2026-03-31';
SET CLINICAL_LOOKBACK_YEARS = -2;
SET BNF_PREFIX = '0403';
SET CLUSTER_ID = 'DEPR_COD';
WITH practices AS (
-- Default reporting geography: active Norfolk and Suffolk parent GP practices.
SELECT DISTINCT
"OrganisationCode" AS "PracticeCode",
"OrganisationName" AS "PracticeName",
"PCNName",
"PlaceName",
"AllianceName"
FROM DATA_HUB.DWH."DimOrganisationAndSite"
WHERE "OrganisationSubType" = 'GP Practice'
AND "IsSiteActive" = 'Yes'
AND "IsSiteNorfolkAndSuffolk" = 'Yes'
AND "SiteCode" = "OrganisationCode"
),
base_population AS (
-- Active, known, living patients registered to the selected practices.
SELECT
p."PracticeCode",
p."PracticeName",
p."PCNName",
p."PlaceName",
p."AllianceName",
dp."PersonKey",
dp."PatientPseudonym",
dp."CurrentAge"
FROM DATA_HUB.DWH."DimPerson" dp
INNER JOIN practices p
ON dp."CurrentGeneralPractice" = p."PracticeCode"
WHERE dp."RecordStatus" = 'Active'
AND dp."PersonStatus" = 'Known'
AND dp."YearMonthDeath" IS NULL
),
medicine_products AS (
-- Replace with VTM/VMP/explicit product logic if BNF prefix is too broad.
SELECT DISTINCT "ProductSnomedCode"
FROM DATA_HUB.DWH."DimMedicineAndDevice"
WHERE "BNFCode" LIKE $BNF_PREFIX || '%'
),
clinical_codes AS (
-- Maintained cluster definitions avoid fragile free-text SNOMED searches.
SELECT DISTINCT "SNOMEDCode"
FROM DATA_HUB.PHM."ClinicalCodingClusterSnomedCodes"
WHERE "Cluster_ID" = $CLUSTER_ID
),
prescribed_patients AS (
-- Medicine-exposed patients in the prescribing window.
SELECT DISTINCT
bp."PracticeCode",
bp."PracticeName",
bp."PCNName",
bp."PlaceName",
bp."AllianceName",
bp."PersonKey",
bp."PatientPseudonym",
bp."CurrentAge"
FROM REPORTING_DATASETS_ICB.SCRATCHPAD."MEDS__UnifiedPrescribingTable" rx
INNER JOIN base_population bp
ON rx."PersonKey" = bp."PersonKey"
INNER JOIN medicine_products mp
ON rx."SNOMEDCode" = mp."ProductSnomedCode"
WHERE rx."DateMedicationStart" BETWEEN $PRESCRIBING_START_DATE AND $PRESCRIBING_END_DATE
),
coded_patients AS (
-- Look back from the end of the prescribing period for matching clinical events.
SELECT DISTINCT
pp."PersonKey",
MIN(cc."EventDateTime"::DATE) AS "FirstMatchingClinicalEventDate",
MAX(cc."EventDateTime"::DATE) AS "LatestMatchingClinicalEventDate"
FROM prescribed_patients pp
INNER JOIN DATA_HUB.PHM."PrimaryCareClinicalCoding" cc
ON pp."PatientPseudonym" = cc."PatientPseudonym"
INNER JOIN clinical_codes c
ON cc."SNOMEDCode" = c."SNOMEDCode"
WHERE cc."EventDateTime"::DATE BETWEEN DATEADD('YEAR', $CLINICAL_LOOKBACK_YEARS, $PRESCRIBING_END_DATE::DATE)
AND $PRESCRIBING_END_DATE::DATE
GROUP BY pp."PersonKey"
)
SELECT
pp."PracticeCode",
pp."PracticeName",
pp."PCNName",
pp."PlaceName",
pp."AllianceName",
pp."PersonKey",
pp."CurrentAge",
CASE WHEN cp."PersonKey" IS NOT NULL THEN 1 ELSE 0 END AS "HasClinicalCodeInLookback",
cp."FirstMatchingClinicalEventDate",
cp."LatestMatchingClinicalEventDate"
FROM prescribed_patients pp
LEFT JOIN coded_patients cp
ON pp."PersonKey" = cp."PersonKey"
ORDER BY
pp."PracticeName",
pp."PersonKey";