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,90 @@
-- ============================================================================
-- MEDS__ProductPriceAndUnitLookup
-- Medicine Reference Data: Price Per Unit and Pack Unit Descriptions
-- ============================================================================
-- Derives PricePerUnit and PackUnitDescription for all dm+d product levels
-- For VMPP/AMPP: uses direct values from DimMedicineAndDevice
-- For VMP/AMP: derives from child VMPP/AMPP products
-- - Price: AVG of child prices
-- - Units: MODE (most common) of child units
-- Price priority: DrugTariffPricePerUnit > IndicativePricePerUnit > AnnualCost/AnnualQuantity
-- ============================================================================
WITH PackLevelData AS (
-- Get prices and units for pack-level products (VMPP/AMPP)
SELECT
"ProductSnomedCode",
"ParentPresentationSnomedCode",
"ProductLevel",
"PackUnitDescription",
COALESCE(
"DrugTariffPricePerUnit",
"IndicativePricePerUnit",
CASE WHEN "AnnualQuantity" > 0 THEN "AnnualCost" / "AnnualQuantity" END
) AS PricePerUnit
FROM DATA_HUB.DWH."DimMedicineAndDevice"
WHERE "ProductLevel" IN ('VMPP', 'AMPP')
),
ProductPrices AS (
-- Direct prices for VMPP/AMPP
SELECT
"ProductSnomedCode",
PricePerUnit
FROM PackLevelData
WHERE PricePerUnit IS NOT NULL
UNION ALL
-- Aggregated prices for VMP/AMP from their children
SELECT
parent."ProductSnomedCode",
AVG(pld.PricePerUnit) AS PricePerUnit
FROM DATA_HUB.DWH."DimMedicineAndDevice" parent
JOIN PackLevelData pld ON pld."ParentPresentationSnomedCode" = parent."ProductSnomedCode"
WHERE parent."ProductLevel" IN ('VMP', 'AMP')
AND pld.PricePerUnit IS NOT NULL
GROUP BY parent."ProductSnomedCode"
),
PackUnits AS (
-- Direct units for VMPP/AMPP
SELECT
"ProductSnomedCode",
"PackUnitDescription"
FROM PackLevelData
WHERE "PackUnitDescription" IS NOT NULL
UNION ALL
-- Units for VMP/AMP: use most common unit from child VMPP/AMPP products
SELECT
parent."ProductSnomedCode",
MODE(pld."PackUnitDescription") AS "PackUnitDescription"
FROM DATA_HUB.DWH."DimMedicineAndDevice" parent
JOIN PackLevelData pld ON pld."ParentPresentationSnomedCode" = parent."ProductSnomedCode"
WHERE parent."ProductLevel" IN ('VMP', 'AMP')
AND pld."PackUnitDescription" IS NOT NULL
GROUP BY parent."ProductSnomedCode"
)
-- ============================================================================
-- Usage: Join these CTEs to prescribing data on ProductSnomedCode
-- ============================================================================
-- Example:
-- SELECT
-- prescribing.*,
-- pp.PricePerUnit,
-- pu."PackUnitDescription",
-- ROUND(pp.PricePerUnit * prescribing."Quantity", 2) AS EstPrice
-- FROM [prescribing_data] prescribing
-- LEFT JOIN ProductPrices pp ON prescribing."SNOMEDCode" = pp."ProductSnomedCode"
-- LEFT JOIN PackUnits pu ON prescribing."SNOMEDCode" = pu."ProductSnomedCode"
-- ============================================================================
SELECT
pp."ProductSnomedCode",
pp.PricePerUnit,
pu."PackUnitDescription"
FROM ProductPrices pp
LEFT JOIN PackUnits pu ON pp."ProductSnomedCode" = pu."ProductSnomedCode"
@@ -0,0 +1,22 @@
-- =============================================================================
-- Run BOTH queries and import into a sheet called "Lookup":
-- - Column A = Brand Name (from Query 1)
-- - Column C = Generic Name (from Query 2)
-- =============================================================================
-- QUERY 1: Brand Names -> paste into Column A of "Lookup" sheet
SELECT DISTINCT
mad."ProductDescription" AS "Brand Name"
FROM DATA_HUB.DWH."DimMedicineAndDevice" mad
WHERE mad."ProductDescription" IS NOT NULL
ORDER BY mad."ProductDescription";
-- QUERY 2: Generic Names -> paste into Column C of "Lookup" sheet
SELECT DISTINCT
gen."ProductDescription" AS "Generic Name"
FROM DATA_HUB.DWH."DimMedicineAndDevice" mad
INNER JOIN DATA_HUB.DWH."DimMedicineAndDevice" gen
ON mad."MedicinalLatestSnomedCode" = gen."ProductSnomedCode"
WHERE gen."ProductDescription" IS NOT NULL
ORDER BY gen."ProductDescription";
@@ -0,0 +1,9 @@
-- Latest Data Query for Snowflake
-- Returns the most recent prescribing system event date.
-- DateMedicationStart can include future-dated medication records, so this uses
-- DateEventRecorded as the data freshness marker.
SELECT
MAX(CAST("DateEventRecorded" AS DATE)) AS "LatestEventRecordedDate"
FROM PRIMARY_CARE.TPP."SRPrimaryCareMedication"
WHERE "DateEventRecorded" >= DATEADD('MONTH', -3, CURRENT_DATE());
@@ -0,0 +1,24 @@
/*
================================================================================
HF_07_PracticeList.sql
Distinct list of GP Practices in Norfolk & Waveney ICB
================================================================================
PURPOSE:
Reference list of GP practice codes and names for the heart failure analysis.
CREATED: 2026-01-13
================================================================================
*/
SELECT DISTINCT
org."SiteCode" AS "Practice Code",
org."OrganisationName" AS "Practice Name",
org."PlaceName" AS "Place",
org."PCNName" AS "PCN"
FROM DATA_HUB.DWH."DimOrganisationAndSite" org
WHERE org."IsSiteActive" = 'Yes'
AND org."SiteType" = 'Parent'
AND org."IsSiteNorfolkAndWaveney" = 'Yes'
AND org."OrganisationSubType" = 'GP Practice'
ORDER BY org."OrganisationName";
@@ -0,0 +1,12 @@
-- REGISTERED POPULATION: Practice population counts for all Norfolk & Waveney GP practices
-- Returns OrganisationName and RegisteredPopulation for reporting and per-capita calculations
SELECT DISTINCT
"OrganisationName",
"RegisteredPopulation"
FROM DATA_HUB.DWH."DimOrganisationAndSite"
WHERE "IsSiteNorfolkAndWaveney" = 'Yes'
AND "IsSiteActive" = 'Yes'
AND "OrganisationSubType" = 'GP Practice'
AND "OrganisationName" <> 'Vulnerable Adults Service'
ORDER BY "OrganisationName"