Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Help with query to find free time in a patient's schedule Expand / Collapse
Author
Message
Posted Tuesday, July 8, 2014 2:22 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, August 22, 2014 10:01 AM
Points: 2, Visits: 69
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.
Post #1590524
Posted Tuesday, July 8, 2014 2:39 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:14 PM
Points: 12,952, Visits: 32,476
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

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1590529
Posted Tuesday, July 8, 2014 5:11 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, August 22, 2014 10:01 AM
Points: 2, Visits: 69
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
Post #1590562
Posted Thursday, July 10, 2014 4:36 PM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, November 26, 2014 3:19 PM
Points: 157, Visits: 608
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
Post #1591364
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse