Solved

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

Posted on 2011-09-21
2
301 Views
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.PatientID
Signals.Time (when the vital sign is charted)
Signals.Value Expected table result
0
Comment
Question by:Warfarin162
2 Comments
 
LVL 69

Accepted Solution

by:
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

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

Open in new window

0
 

Author Comment

by:Warfarin162
ID: 36575846
Coalesce!  This works perfectly!

Thanks for teaching me a cool new trick :)
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

831 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