SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Help with query to find free time in a patient's schedule


Help with query to find free time in a patient's schedule

Author
Message
cmbrown
cmbrown
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 77
Hi,
I have query that returns the scheduled events for a patient. I need to find a way to report the free time in the patient's schedule so we can assign other events or activities.

Here is a sample of the data:
PtID ApptDate Activity FromToTime Location
123 7/7/2014 0:00 Opening Group 9:00 - 9:30a XYZ Campus
123 7/7/2014 0:00 Opening Group 9:30 - 10:00a XYZ Campus
123 7/7/2014 0:00 1:1 ST 10:30 - 11:00a XYZ Campus
123 7/7/2014 0:00 Conditioning Group 11:00 - 11:30a XYZ Campus
123 7/7/2014 0:00 Conditioning Group 11:30a - 12:00p XYZ Campus
123 7/7/2014 0:00 Menu Planning Group 1:00 - 1:30p XYZ Campus
123 7/7/2014 0:00 Menu Planning Group 1:30 - 2:00p XYZ Campus
123 7/7/2014 0:00 Life 101 Group 3:00 - 3:30p XYZ Campus
123 7/7/2014 0:00 Life 101 Group 3:30 - 4:00p XYZ Campus
123 7/7/2014 0:00 Medical Appt 4:00 - 4:30p XYZ Campus
123 7/7/2014 0:00 Medical Appt 4:30 - 5:00p XYZ Campus
123 7/7/2014 0:00 Laundry Day 5:00 - 5:30p XYZ Campus

The query I need would return
123, 7/7/2014, 10:00 - 10:30
123, 7/7/2014, 12:00 PM - 1:00 PM

I am using SQL Server 2005
the raw data for all dates and start and end times for the appointments are datetime datatypes

Any help would be greatly appreciated.
Lowell
Lowell
One Orange Chip
One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)

Group: General Forum Members
Points: 28203 Visits: 39954
well, it's not going to be easy, but this will get you started;
because you are not using the right data types (a string containing '9:00 - 9:30a' instead of two fields, a start time and end time of datatype time,you have
to chop the data up, and cast it to time.
then you can do gaps and islands logic to find open times.

you said above that your start and ends were dates, but that's not what exists int he sample data.
do exactly like i did, and provide sample data that is copy-and-pasteable to SSMS, and we can actually help with your example.




;WITH MyCTE([PtID],[ApptDate],[Activity],[FromToTime],[Location])
AS
(
SELECT '123','7/7/2014 0:00','Opening Group','9:00 - 9:30a','XYZ Campus' UNION ALL
SELECT '123','7/7/2014 0:00','Opening Group','9:30 - 10:00a','XYZ Campus' UNION ALL
SELECT '123','7/7/2014 0:00','1:1 ST','10:30 - 11:00a','XYZ Campus' UNION ALL
SELECT '123','7/7/2014 0:00','Conditioning Group','11:00 - 11:30a','XYZ Campus' UNION ALL
SELECT '123','7/7/2014 0:00','Conditioning Group','11:30a - 12:00p','XYZ Campus' UNION ALL
SELECT '123','7/7/2014 0:00','Menu Planning Group','1:00 - 1:30p','XYZ Campus' UNION ALL
SELECT '123','7/7/2014 0:00','Menu Planning Group','1:30 - 2:00p','XYZ Campus' UNION ALL
SELECT '123','7/7/2014 0:00','Life 101 Group','3:00 - 3:30p','XYZ Campus' UNION ALL
SELECT '123','7/7/2014 0:00','Life 101 Group','3:30 - 4:00p','XYZ Campus' UNION ALL
SELECT '123','7/7/2014 0:00','Medical Appt','4:00 - 4:30p','XYZ Campus' UNION ALL
SELECT '123','7/7/2014 0:00','Medical Appt','4:30 - 5:00p','XYZ Campus' UNION ALL
SELECT '123','7/7/2014 0:00','Laundry Day','5:00 - 5:30p','XYZ Campus'
)
SELECT *,fn1.Item As StartTime,fn2.Item AS EndTime
FROM MyCTE
CROSS APPLY(SELECT Item FROM dbo.DelimitedSplit8K([FromToTime],'-')fn WHERE ITemNumber = 1) fn1
CROSS APPLY(SELECT Item FROM dbo.DelimitedSplit8K([FromToTime],'-')fn WHERE ITemNumber = 2) fn2



Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

cmbrown
cmbrown
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 77
Thanks much Lowell.
I thought about that on my way home. The TimeFromTo was from a view that cast the times as varchar and concatenated to a single string. Here is the actual data:
PtID ApptDate ApptDate TimeFrom TimeTo Activity
1424 7/7/2014 0:00 12/30/1899 9:00:00 AM 12/30/1899 10:00:00 AM Opening Group
1424 7/7/2014 0:00 12/30/1899 10:00:00 AM 12/30/1899 11:00:00 AM 1:1 ST Jerilyn
1424 7/7/2014 0:00 12/30/1899 11:00:00 AM 12/30/1899 12:00:00 PM Conditioning Group
1424 7/7/2014 0:00 12/30/1899 1:00:00 PM 12/30/1899 2:00:00 PM Menu Planning Group
1424 7/7/2014 0:00 12/30/1899 3:00:00 PM 12/30/1899 4:00:00 PM Life 101 Group
1424 7/7/2014 0:00 12/30/1899 4:15:00 PM 12/30/1899 4:45:00 PM Medical Appt
1424 7/7/2014 0:00 12/30/1899 5:00:00 PM 12/30/1899 5:15:00 PM Laundry Day

Chris
autoexcrement
autoexcrement
Old Hand
Old Hand (376 reputation)Old Hand (376 reputation)Old Hand (376 reputation)Old Hand (376 reputation)Old Hand (376 reputation)Old Hand (376 reputation)Old Hand (376 reputation)Old Hand (376 reputation)

Group: General Forum Members
Points: 376 Visits: 777
If you can post some DDL to create/insert the tables/data--the underlying data, not the concatenated string version--I'm sure someone will be able to help you.

http://www.sqlservercentral.com/articles/Best+Practices/61537/


"If I had been drinking out of that toilet, I might have been killed." -Ace Ventura
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search