Creating a row of data for every minute between two known dates

Posted on 2011-09-21
Last Modified: 2012-05-12
I am working in a medical database.  I want to summarize multiple vital signs (blood pressure, pulse, oxygen saturation, etc) into a single table, that show the values for each minute in a new row.  (Our EHR populates a value once a minute max).  The data is currently stored as ROWS in the table "Signals".... I am interested in grouping some vitals that occurred at the same time (putting results into COLUMNS).

The issue:  I can't simply use the timestamp of any given vital sign to connect the other vitals from that minute together, because no single vital sign is ALWAYS documented in every minute.  For instance, blood pressure is usually every 5 minutes, while pulse is usually every 1 minute (unless there is an issue with the monitor......).

I think if I can create a column of datetimes with 1 minute between each one, I could then connect the other vital signs at a mimute to the same row, since "PatientID" and "Time" could combine into a unique key for the added parameter.  The 1 minute intervals need to START at "AnesthesiaStartTime", and END at "AnesthesiaEndTime"

#AnesthesiaTimes.AnesthesiaStartTime - Defined in a temporary table

#AnesthesiaTimes.AnesthesiaEndTime - Defined in a temporary table

Final table will hopefully look something like the image attached (with more values tagged on to the end)

The items of interest are all found in the table "Signals"
Signals.Time (when the vital sign is charted)
Signals.Value Expected table result
Question by:Warfarin162
LVL 69

Accepted Solution

Qlemo earned 500 total points
ID: 36575728
One way without need for generating intermediate pseudo-records is to use an outer join, using the time. However, that requires that probes are taken exactly on the same time, if taken. We can remove that restriction by e.g. cutting off the seconds, to make it more fuzzy.

First, you build a select for each particular vital sign - you will certainly have an ID to differ between blood pressure and pulse aso, and I will assume it is called "type".
Then we use those selects as tables, and join them together:
select at.patientID, coalesce(bp.time, p.time) as Time, bp.value as [Blood Pressure], p.value as [Pulse]
from #AnesthesiaTimes at
left join (select patientID, time, value from signals where type = 'Blood Pressure') bp
  on at.patientID = bp.patientID and bp.time between at.AnesthesiaStartTime and at.AnesthesiaEndTime
left join (select patientID, time, value from signals where type = 'Pulse') p
  on at.patientID = p.patientID  and  p.time between at.AnesthesiaStartTime and at.AnesthesiaEndTime
 and bp.time = p.time 

Open in new window

You should get rid of seconds, if needed, if we change
  and bp.time = p.time

Open in new window

  and bp.time - datepart(ss, bp.time) = p.time - datepart(ss, p.time)

Open in new window


Author Comment

ID: 36575846
Coalesce!  This works perfectly!

Thanks for teaching me a cool new trick :)

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question