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,78 @@
/*
Dispensing by VTM or VMP
========================
Purpose:
Summarise dispensed items, patients, and quantity for a medicine group
using national dispensing data.
Data source:
NATIONAL.GPMED."MedicinesDispensedInPrimarycare"
Notes:
- Dispensing data is official/payment-oriented and usually lags prescribing.
- Set either VTM_SNOMED_CODE or VMP_SNOMED_CODE. Leave the other as NULL.
- ProcessingPeriodDate is a month-level period date.
*/
SET START_PERIOD = '2025-04-01';
SET END_PERIOD = '2026-03-31';
SET VTM_SNOMED_CODE = NULL;
SET VMP_SNOMED_CODE = 'REPLACE_WITH_VMP_SNOMED_CODE';
WITH products AS (
-- Expands the selected VTM/VMP into product-level dm+d codes found in GPMED.
SELECT DISTINCT
"ProductSnomedCode",
"ProductDescription",
"TherapeuticMoietyName",
"BNFCode"
FROM DATA_HUB.DWH."DimMedicineAndDevice"
WHERE ($VTM_SNOMED_CODE IS NOT NULL AND "TherapeuticMoietySnomedCode" = $VTM_SNOMED_CODE)
OR ($VMP_SNOMED_CODE IS NOT NULL AND "MedicinalLatestSnomedCode" = $VMP_SNOMED_CODE)
OR ($VMP_SNOMED_CODE IS NOT NULL AND "ProductSnomedCode" = $VMP_SNOMED_CODE)
),
practices AS (
-- Restricts output 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 "OrganisationSubType" = 'GP Practice'
AND "IsSiteActive" = 'Yes'
AND "IsSiteNorfolkAndSuffolk" = 'Yes'
AND "SiteCode" = "OrganisationCode"
)
SELECT
gpm."ProcessingPeriodDate" AS "PeriodDate",
p."PracticeCode",
p."PracticeName",
p."PCNName",
p."PlaceName",
p."AllianceName",
prod."TherapeuticMoietyName",
COUNT(DISTINCT gpm."PatientPseudonym") AS "Patients",
SUM(gpm."ItemCount") AS "Items",
SUM(gpm."PaidQuantity") AS "PaidQuantity"
FROM NATIONAL.GPMED."MedicinesDispensedInPrimarycare" gpm
INNER JOIN products prod
ON gpm."PaiddmdCode" = prod."ProductSnomedCode"
INNER JOIN practices p
ON gpm."CostCentreODSCode" = p."PracticeCode"
WHERE gpm."ProcessingPeriodDate" BETWEEN $START_PERIOD AND $END_PERIOD
AND gpm."PatientPseudonym" IS NOT NULL
GROUP BY
gpm."ProcessingPeriodDate",
p."PracticeCode",
p."PracticeName",
p."PCNName",
p."PlaceName",
p."AllianceName",
prod."TherapeuticMoietyName"
ORDER BY
"PeriodDate",
"PracticeName",
prod."TherapeuticMoietyName";
@@ -0,0 +1,49 @@
/*
Medicine reference lookup
=========================
Purpose:
Search DATA_HUB.DWH."DimMedicineAndDevice" for dm+d products and their
related BNF, VTM, VMP, AMP, and pack-level identifiers.
Common uses:
- Find the SNOMED product codes to use in a prescribing or dispensing query.
- Check whether a medicine group is better captured by BNF, VTM, VMP, or
explicit product codes.
- Understand how products roll up through the dm+d hierarchy.
Replace the SEARCH_TEXT and optional BNF_PREFIX before running.
*/
SET SEARCH_TEXT = 'tirzepatide';
SET BNF_PREFIX = NULL;
SELECT DISTINCT
med."ProductSnomedCode",
med."ProductDescription",
med."ProductLevel",
med."TherapeuticMoietySnomedCode",
med."TherapeuticMoietyName",
med."MedicinalLatestSnomedCode",
med."ParentPresentationSnomedCode",
med."BNFCode",
med."BNFParagraphCode",
med."RouteName",
med."StrengthDescription",
med."PackUnitDescription",
med."IndicativePricePerUnit",
med."DrugTariffPricePerUnit"
FROM DATA_HUB.DWH."DimMedicineAndDevice" med
WHERE (
LOWER(med."ProductDescription") LIKE '%' || LOWER($SEARCH_TEXT) || '%'
OR LOWER(med."TherapeuticMoietyName") LIKE '%' || LOWER($SEARCH_TEXT) || '%'
OR med."ProductSnomedCode" = $SEARCH_TEXT
OR med."TherapeuticMoietySnomedCode" = $SEARCH_TEXT
OR med."MedicinalLatestSnomedCode" = $SEARCH_TEXT
)
AND ($BNF_PREFIX IS NULL OR med."BNFCode" LIKE $BNF_PREFIX || '%')
ORDER BY
med."TherapeuticMoietyName",
med."MedicinalLatestSnomedCode",
med."ProductLevel",
med."ProductDescription";
@@ -0,0 +1,71 @@
/*
Current prescribing by VMP
==========================
Purpose:
Return patient-level prescribing records for all products under one
Virtual Medicinal Product (VMP).
Use this when a VTM is too broad and you need a specific formulation,
strength, or medicinal product family.
*/
SET START_DATE = '2025-04-01';
SET END_DATE = '2026-03-31';
SET VMP_SNOMED_CODE = 'REPLACE_WITH_VMP_SNOMED_CODE';
WITH products AS (
-- Expands one VMP to all matching product SNOMED codes in the medicine dimension.
SELECT DISTINCT
"ProductSnomedCode",
"ProductDescription",
"ProductLevel",
"MedicinalLatestSnomedCode",
"TherapeuticMoietyName",
"BNFCode",
"BNFParagraphCode"
FROM DATA_HUB.DWH."DimMedicineAndDevice"
WHERE "MedicinalLatestSnomedCode" = $VMP_SNOMED_CODE
OR "ProductSnomedCode" = $VMP_SNOMED_CODE
),
practices AS (
-- Restricts output 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 "OrganisationSubType" = 'GP Practice'
AND "IsSiteActive" = 'Yes'
AND "IsSiteNorfolkAndSuffolk" = 'Yes'
AND "SiteCode" = "OrganisationCode"
)
SELECT DISTINCT
p."PracticeCode",
p."PracticeName",
p."PCNName",
p."PlaceName",
p."AllianceName",
rx."PersonKey",
rx."DateMedicationStart",
prod."ProductDescription",
prod."ProductLevel",
prod."TherapeuticMoietyName",
prod."BNFCode",
TRY_CAST(rx."Quantity" AS FLOAT) AS "Quantity",
rx."QuantityUnit",
rx."Directions",
rx."SourceSystem"
FROM REPORTING_DATASETS_ICB.SCRATCHPAD."MEDS__UnifiedPrescribingTable" rx
INNER JOIN products prod
ON rx."SNOMEDCode" = prod."ProductSnomedCode"
INNER JOIN practices p
ON rx."CurrentGeneralPractice" = p."PracticeCode"
WHERE rx."DateMedicationStart" BETWEEN $START_DATE AND $END_DATE
AND rx."PersonKey" IS NOT NULL
ORDER BY
p."PracticeName",
rx."PersonKey",
rx."DateMedicationStart";
@@ -0,0 +1,74 @@
/*
Current prescribing by VTM
=========================
Purpose:
Return patient-level prescribing records for all products under one
Virtual Therapeutic Moiety (VTM).
Data source:
REPORTING_DATASETS_ICB.SCRATCHPAD."MEDS__UnifiedPrescribingTable"
Notes:
- VTM is useful when you want all products containing the same therapeutic
moiety, regardless of brand, strength, or pack.
- The practice CTE is deliberately visible. Change the geography filter
there if the report should not be Norfolk and Suffolk.
*/
SET START_DATE = '2025-04-01';
SET END_DATE = '2026-03-31';
SET VTM_SNOMED_CODE = 'REPLACE_WITH_VTM_SNOMED_CODE';
WITH products AS (
-- Expands one VTM to all matching product SNOMED codes in the medicine dimension.
SELECT DISTINCT
"ProductSnomedCode",
"ProductDescription",
"TherapeuticMoietyName",
"BNFCode",
"BNFParagraphCode"
FROM DATA_HUB.DWH."DimMedicineAndDevice"
WHERE "TherapeuticMoietySnomedCode" = $VTM_SNOMED_CODE
),
practices AS (
-- Restricts output 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 "OrganisationSubType" = 'GP Practice'
AND "IsSiteActive" = 'Yes'
AND "IsSiteNorfolkAndSuffolk" = 'Yes'
AND "SiteCode" = "OrganisationCode"
)
SELECT DISTINCT
p."PracticeCode",
p."PracticeName",
p."PCNName",
p."PlaceName",
p."AllianceName",
rx."PersonKey",
rx."DateMedicationStart",
prod."ProductDescription",
prod."TherapeuticMoietyName",
prod."BNFCode",
TRY_CAST(rx."Quantity" AS FLOAT) AS "Quantity",
rx."QuantityUnit",
rx."Directions",
rx."IsRepeatPrescription",
rx."SourceSystem"
FROM REPORTING_DATASETS_ICB.SCRATCHPAD."MEDS__UnifiedPrescribingTable" rx
INNER JOIN products prod
ON rx."SNOMEDCode" = prod."ProductSnomedCode"
INNER JOIN practices p
ON rx."CurrentGeneralPractice" = p."PracticeCode"
WHERE rx."DateMedicationStart" BETWEEN $START_DATE AND $END_DATE
AND rx."PersonKey" IS NOT NULL
ORDER BY
p."PracticeName",
rx."PersonKey",
rx."DateMedicationStart";
@@ -0,0 +1,41 @@
/*
Prescribing for a patient pseudonym
===================================
Purpose:
Pull a concise prescribing history for one pseudonym.
Use carefully:
- Do not commit real patient pseudonyms into shared repos.
- Keep row-level extracts out of git and shared folders unless there is a
clear information governance basis.
*/
SET START_DATE = '2025-04-01';
SET END_DATE = '2026-03-31';
SET PATIENT_PSEUDONYM = 'REPLACE_WITH_PATIENT_PSEUDONYM';
SELECT DISTINCT
dp."PersonKey",
dp."PatientPseudonym",
gp."OrganisationName" AS "CurrentGeneralPracticeName",
rx."DateMedicationStart",
med."ProductDescription",
med."TherapeuticMoietyName",
med."BNFCode",
CAST(rx."Quantity" AS FLOAT) AS "Quantity",
rx."QuantityUnit",
rx."Directions",
rx."IsRepeatPrescription",
rx."SourceSystem"
FROM REPORTING_DATASETS_ICB.SCRATCHPAD."MEDS__UnifiedPrescribingTable" rx
INNER JOIN DATA_HUB.DWH."DimPerson" dp
ON rx."PersonKey" = dp."PersonKey"
LEFT JOIN DATA_HUB.DWH."DimMedicineAndDevice" med
ON rx."SNOMEDCode" = med."ProductSnomedCode"
LEFT JOIN DATA_HUB.DWH."DimOrganisationAndSite" gp
ON rx."CurrentGeneralPractice" = gp."OrganisationCode"
AND gp."SiteCode" = gp."OrganisationCode"
WHERE dp."PatientPseudonym" = $PATIENT_PSEUDONYM
AND rx."DateMedicationStart" BETWEEN $START_DATE AND $END_DATE
ORDER BY rx."DateMedicationStart", med."ProductDescription";