top of page

Welcome
to NumpyNinja Blogs

NumpyNinja: Blogs. Demystifying Tech,

One Blog at a Time.
Millions of views. 

How to Track Patient Trends Over Time with SQL Window Functions

In healthcare data analysis, understanding how patient metrics change over time isn't just useful, it's critical for effective treatment. Are glucose levels improving? Is blood pressure trending in the right direction? These temporal patterns hold the key to personalized care. While basic SQL aggregations can tell you what's happening, window functions unlock the capability to see how things are changing. Let's explore how these powerful SQL features can transform raw patient data into actionable clinical insights.

 

Consider a patient glucose monitoring table:

 

SELECT * FROM dexcom;


ree

 

Looking at the readings at an individual level has limited value, is time consuming and is simply not feasible if the output is anything more than a handful of rows long. How can we work with a large dataset that has temporal data and extract meaningful insights that are understandable, while maintaining individual level granularity in the results?

 

Window Functions: A Time-traveling Accessory


Window functions enable advanced analytical calculations across row sets while preserving individual row context. Unlike aggregate functions that condense multiple rows into single results, window operations maintain the original dataset's granularity and only perform computations on the particular frame of data.

 

Here’s a simple example of what that means:

 

SELECT patient_id, time_of_event, glucose_value_mgdl,

LAG(glucose_value_mgdl)

OVER (PARTITION BY patient_id ORDER BY time_of_event) AS previous_reading

FROM dexcom;


 

ree

The LAG() function looks back one row in the sequence – in this case the previous glucose value for each patient in the time series data. And instantly it provides a point-in-time comparison between past and current data, by establishing a historical reference point for each glucose reading.The PARTITION BY clause ensures each patient’s trend is calculated independently. Without it, the comparisons would be made across different patients’ readings – which would make no clinical sense.

 

Real-World Example: Weekly Glucose Trend Analysis:


Let’s build a comprehensive trend analysis using continuous glucose monitoring (CGM) data that shows a patient’s past and current week averages, weekly Time-In-Range values, hyperglycemic event data if any and the overall trend direction the patient is moving towards. One key consideration is to always filter before windowing by using CTEs or subqueries, to reduce the size of the dataset first.

 

Defining the various metrics here:

 

 

ree

Setting the conditions for trend analysis:

 

ree

 

Here’s what the results look like:

ree

 

 

We can now see a snapshot of each patient’s health metrics and progress trend through each week- from the time they were admitted into the study till the end of the week, and are able to immediately decide if a patient needs additional clinical monitoring or medical intervention. With the help of window functions, we have transformed raw, tedious data into a meaningful story of a patient’s health journey and their progress.

 
 

+1 (302) 200-8320

NumPy_Ninja_Logo (1).png

Numpy Ninja Inc. 8 The Grn Ste A Dover, DE 19901

© Copyright 2025 by Numpy Ninja Inc.

  • Twitter
  • LinkedIn
bottom of page