/* 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";