Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #200 from rw251/update-height-query
Update height and weight queries
- Loading branch information
Showing
4 changed files
with
167 additions
and
15 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
shared/Reusable queries for data extraction/query-get-closest-diagnosis-to-date.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
{if:verbose} | ||
--┌───────────────────────────────────────────────────┐ | ||
--│ Find the closest diagnosis to a particular date │ | ||
--└───────────────────────────────────────────────────┘ | ||
|
||
-- OBJECTIVE: To find the closest diagnosis for a particular disease and a given date. | ||
|
||
-- INPUT: A variable: | ||
-- - date: date - (YYYY-MM-DD) the date to look around | ||
-- - comparison: inequality sign (>, <, >= or <=) e.g. if '>' then will look for the first value strictly after the date | ||
-- - 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 | ||
-- - code-set: string - the name of the code set to be used. Must be one from the repository. | ||
-- - version: number - the code set version | ||
-- - temp-table-name: string - the name of the temp table that this will produce | ||
|
||
-- OUTPUT: Temp tables as follows: | ||
-- (temp table name specified in parameter) FK_Patient_Link_ID, EventDate | ||
{endif:verbose} | ||
|
||
--> CODESET {param:code-set}:{param:version} | ||
|
||
-- First we get the date of the nearest {param:code-set} diagnosis before/after the specified date | ||
|
||
IF OBJECT_ID('tempdb..{param:temp-table-name}') IS NOT NULL DROP TABLE {param:temp-table-name}; | ||
{if:comparison=>} | ||
SELECT FK_Patient_Link_ID, MIN(EventDate) AS EventDate | ||
{endif:comparison} | ||
{if:comparison=>=} | ||
SELECT FK_Patient_Link_ID, MIN(EventDate) AS EventDate | ||
{endif:comparison} | ||
{if:comparison=<} | ||
SELECT FK_Patient_Link_ID, MAX(EventDate) AS EventDate | ||
{endif:comparison} | ||
{if:comparison=<=} | ||
SELECT FK_Patient_Link_ID, MAX(EventDate) AS EventDate | ||
{endif:comparison} | ||
INTO {param:temp-table-name} | ||
FROM {param:gp-events-table} | ||
WHERE SuppliedCode IN (SELECT code FROM #AllCodes WHERE Concept = '{param:code-set}' AND Version = {param:version}) | ||
AND EventDate {param:comparison} '{param:date}' | ||
{if:patients} | ||
AND p.FK_Patient_Link_ID IN (SELECT FK_Patient_Link_ID FROM {param:patients}) | ||
{endif:patients} | ||
{if:all-patients=false} | ||
AND p.FK_Patient_Link_ID IN (SELECT FK_Patient_Link_ID FROM #Patients) | ||
{endif:all-patients} | ||
GROUP BY FK_Patient_Link_ID; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
shared/Reusable queries for data extraction/query-get-weight.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
{if:verbose} | ||
--┌──────────────────────────┐ | ||
--│ Gets a patient's Weight │ | ||
--└──────────────────────────┘ | ||
|
||
-- OBJECTIVE: Gets the most recent measurement of a person's Weight, in kilograms | ||
|
||
-- INPUT: A variable: | ||
-- - date: date - (yyyy-mm-dd) the date for which you want to find the most recent measurement | ||
-- - 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: Temp table called #PatientWeight with columns: | ||
-- - FK_Patient_Link_ID - unique patient id | ||
-- - WeightInKilograms - int - the most recent Weight measurement before the specified date, in kg | ||
-- - WeightDate - date (YYYY/MM/DD) - the date of the most recent Weight measurement before the specified date | ||
{endif:verbose} | ||
|
||
-- Weight is almost always recorded in kilograms, so | ||
-- first we get the most recent value for Weight where the unit is 'kg' | ||
--> EXECUTE query-get-closest-value-to-date.sql all-patients:false min-value:0.1 max-value:500 unit:kg date:{param:date} comparison:<= gp-events-table:{param:gp-events-table} code-set:weight version:1 temp-table-name:#PatientWeightInKilograms | ||
|
||
-- NB the units are standardised so 'kg' dominates. You do not get units like 'kilograms'. | ||
|
||
-- now include records that don't have a unit value but have a Weight recording (there are only useful records with NULL for unit, not a blank value) | ||
|
||
IF OBJECT_ID('tempdb..#PatientWeightNoUnitsTEMP1') IS NOT NULL DROP TABLE #PatientWeightNoUnitsTEMP1; | ||
SELECT FK_Patient_Link_ID, MAX(EventDate) AS EventDate | ||
INTO #PatientWeightNoUnitsTEMP1 | ||
FROM {param:gp-events-table} | ||
WHERE Units IS NULL | ||
AND Value IS NOT NULL | ||
AND Value <> '' | ||
AND TRY_CONVERT(DECIMAL(10,3), [Value]) BETWEEN 0.1 AND 500 | ||
AND EventDate <= '{param:date}' | ||
AND SuppliedCode IN (SELECT code FROM #AllCodes WHERE Concept = 'weight' AND Version = 1) | ||
{if:all-patients=false} | ||
AND FK_Patient_Link_ID IN (SELECT FK_Patient_Link_ID FROM #Patients) | ||
{endif:all-patients} | ||
GROUP BY FK_Patient_Link_ID | ||
|
||
IF OBJECT_ID('tempdb..#PatientWeightNoUnits') IS NOT NULL DROP TABLE #PatientWeightNoUnits; | ||
SELECT p.FK_Patient_Link_ID, p.EventDate AS DateOfFirstValue, MAX(p.Value) AS [Value] | ||
INTO #PatientWeightNoUnits | ||
FROM {param:gp-events-table} p | ||
INNER JOIN #PatientWeightNoUnitsTEMP1 sub ON sub.FK_Patient_Link_ID = p.FK_Patient_Link_ID AND sub.EventDate = p.EventDate | ||
WHERE SuppliedCode IN (SELECT code FROM #AllCodes WHERE Concept = 'weight' AND Version = 1) | ||
GROUP BY p.FK_Patient_Link_ID, p.EventDate; | ||
|
||
-- Create the output PatientWeight temp table, with vlaues in kg. We combine the kg and 'no unit' tables from above. | ||
IF OBJECT_ID('tempdb..#PatientWeight') IS NOT NULL DROP TABLE #PatientWeight; | ||
SELECT | ||
wkg.FK_Patient_Link_ID, | ||
WeightInKilograms = TRY_CONVERT(DECIMAL(10,3),wkg.[Value]), | ||
WeightDate = wkg.DateOfFirstValue | ||
INTO #PatientWeight | ||
FROM #PatientWeightInKilograms wkg | ||
UNION ALL | ||
SELECT | ||
wno.FK_Patient_Link_ID, | ||
WeightInKilograms = TRY_CONVERT(DECIMAL(10,3),wno.[Value]), | ||
WeightDate = DateOfFirstValue | ||
FROM #PatientWeightNoUnits wno |