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

How to select range of consecutive events Expand / Collapse
Author
Message
Posted Monday, August 5, 2013 1:15 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, June 27, 2014 11:46 AM
Points: 7, Visits: 143
This is my first cry for help, so be gentle with me. :)

I've been searching the forum for a clue but I'm still having trouble figuring out how to produce the query results I need. and boy does it hurt my pride to say that... :)

The data is fairly simple, a Station ID and a Start & End time. (along with a bunch of other data that needs to be added or dissected.)

I have a list of events for every day and I need to identify blocks of two or more consecutive events and return the first start time and the last end time. This would also be the MIN(start) and MAX(End) of those group of consecutive events.

I can identify the first, last and "middle" records by doing a join back to the table linking the start time of one record to the end time of the other, but I can't figure out how to group one "group" of consecutive events, as there could be multiple groups in a day.

There are some events which are "singular", meaning that they have no adjoining records, and the group could be 2 or 20 consecutive events, and could span midnight if a show runs over.

Here is what the sample data would look like and the output I'm trying to accomplish below that.

I can turn all of this into a make table & insert statements if you want data to play with.

I appreciate the help.

Thanks,

Tad



Station Start End
WKBW 7/5/13 11:00 AM 7/5/13 11:30 AM
WKBW 7/5/13 12:00 PM 7/5/13 12:30 PM
WKBW 7/5/13 12:30 PM 7/5/13 1:00 PM
WKBW 7/5/13 1:00 PM 7/5/13 1:30 PM
WKBW 7/5/13 1:30 PM 7/5/13 2:00 PM
WKBW 7/9/13 11:30 AM 7/9/13 12:00 PM
WKBW 7/9/13 12:00 PM 7/9/13 12:30 PM
WKBW 7/12/13 12:30 PM 7/12/13 1:00 PM
WKBW 7/12/13 1:00 PM 7/12/13 1:30 PM
WKBW 7/12/13 1:30 PM 7/12/13 2:00 PM
WKBW 7/17/13 12:00 PM 7/17/13 1:00 PM
WKBW 7/18/13 1:30 PM 7/18/13 2:00 PM
WKBW 7/19/13 12:30 PM 7/19/13 1:00 PM
WKBW 7/19/13 1:00 PM 7/19/13 1:30 PM
WKBW 7/19/13 1:30 PM 7/19/13 2:00 PM
WKBW 7/19/13 3:00 PM 7/19/13 4:00 PM

KFUN 7/22/13 4:30 AM 7/22/13 7:00 AM
KFUN 7/22/13 7:00 AM 7/22/13 9:00 AM
KFUN 7/22/13 12:00 PM 7/22/13 12:30 PM
KFUN 7/22/13 5:00 PM 7/22/13 6:30 PM
KFUN 7/23/13 4:30 AM 7/23/13 7:00 AM
KFUN 7/23/13 7:00 AM 7/23/13 9:00 AM
KFUN 7/23/13 9:30 AM 7/23/13 10:00 AM
KFUN 7/23/13 10:00 AM 7/23/13 10:47 AM
KFUN 7/23/13 10:53 AM 7/23/13 11:18 AM
KFUN 7/23/13 11:18 AM 7/23/13 11:54 AM
KFUN 7/23/13 12:00 PM 7/23/13 12:30 PM
KFUN 7/23/13 5:00 PM 7/23/13 6:30 PM
KFUN 7/23/13 9:00 PM 7/23/13 10:30 PM
KFUN 7/24/13 4:30 AM 7/24/13 7:00 AM
KFUN 7/24/13 7:00 AM 7/24/13 9:00 AM
KFUN 7/24/13 9:00 PM 7/24/13 10:30 PM
KFUN 7/25/13 4:30 AM 7/25/13 7:00 AM
KFUN 7/25/13 7:00 AM 7/25/13 9:00 AM



OUTPUT
KFUN 7/22/13 4:30 AM 7/22/13 9:00 AM
KFUN 7/23/13 4:30 AM 7/23/13 9:00 AM
KFUN 7/23/13 9:30 AM 7/23/13 10:47 AM
KFUN 7/23/13 10:53 AM 7/23/13 11:54 AM
KFUN 7/24/13 4:30 AM 7/24/13 9:00 AM
KFUN 7/25/13 4:30 AM 7/25/13 9:00 AM
WKBW 7/5/13 12:00 PM 7/5/13 2:00 PM
WKBW 7/9/13 11:30 AM 7/9/13 12:30 PM
WKBW 7/12/13 12:30 PM 7/12/13 2:00 PM
WKBW 7/19/13 12:30 PM 7/19/13 2:00 PM
Post #1481057
Posted Monday, August 5, 2013 1:32 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:32 PM
Points: 13,455, Visits: 12,318
Hi and welcome to the forums. What you are describing is generally known as gaps and islands. For finding groups of contiguous dates this article does a great job explaining how to do that.

http://www.sqlservercentral.com/articles/T-SQL/71550/

If you find that you need help putting all the pieces together it would be very helpful if you could post ddl and sample data.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1481064
Posted Monday, August 5, 2013 2:26 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, June 27, 2014 11:46 AM
Points: 7, Visits: 143
Thanks for the reply Sean. I think I have read this article at one time but couldn't figure out how to apply it in this scenario. The problem is that there is nothing to unique records on or to group by. Each combination of Station + Start time will be unique, and since there could be multiple islands per day, you can't group on the date.

Here is the ddl and sample data.

Let me know what creative ideas you have.

Thanks,

Tad


DECLARE @Events TABLE(StationID VARCHAR(10), dtmStart DateTime, dtmEnd DateTime)

INSERT INTO @Events(StationID, dtmStart, dtmEnd)
VALUES
('WKBW', '7/05/13 11:00', '7/05/13 11:30'),
('WKBW', '7/05/13 12:00', '7/05/13 12:30'),
('WKBW', '7/05/13 12:30', '7/05/13 13:00'),
('WKBW', '7/05/13 13:00', '7/5/13 1:30 PM'),
('WKBW', '7/05/13 13:30', '7/5/13 2:00 PM'),
('WKBW', '7/09/13 11:30', '7/09/13 12:00'),
('WKBW', '7/09/13 12:00', '7/09/13 12:30'),
('WKBW', '7/12/13 12:30', '7/12/13 13:00'),
('WKBW', '7/12/13 13:00', '7/12/13 13:30'),
('WKBW', '7/12/13 13:30', '7/12/13 14:00'),
('WKBW', '7/17/13 12:00', '7/17/13 13:00'),
('WKBW', '7/18/13 13:30', '7/18/13 14:00'),
('WKBW', '7/19/13 12:30', '7/19/13 13:00'),
('WKBW', '7/19/13 13:00', '7/19/13 13:30'),
('WKBW', '7/19/13 13:30', '7/19/13 14:00'),
('WKBW', '7/19/13 15:00', '7/19/13 16:00'),
('KFUN', '7/22/13 04:30', '7/22/13 07:00'),
('KFUN', '7/22/13 07:00', '7/22/13 09:00'),
('KFUN', '7/22/13 12:00', '7/22/13 12:30'),
('KFUN', '7/22/13 17:00', '7/22/13 18:30'),
('KFUN', '7/23/13 04:30', '7/23/13 07:00'),
('KFUN', '7/23/13 07:00', '7/23/13 09:00'),
('KFUN', '7/23/13 09:30', '7/23/13 10:00'),
('KFUN', '7/23/13 10:00', '7/23/13 10:47'),
('KFUN', '7/23/13 10:53', '7/23/13 11:18'),
('KFUN', '7/23/13 11:18', '7/23/13 11:54'),
('KFUN', '7/23/13 12:00', '7/23/13 12:30'),
('KFUN', '7/23/13 17:00', '7/23/13 18:30'),
('KFUN', '7/23/13 21:00', '7/23/13 22:30'),
('KFUN', '7/24/13 04:30', '7/24/13 07:00'),
('KFUN', '7/24/13 07:00', '7/24/13 09:00'),
('KFUN', '7/24/13 21:00', '7/24/13 22:30'),
('KFUN', '7/25/13 04:30', '7/25/13 07:00'),
('KFUN', '7/25/13 07:00', '7/25/13 09:00')
Post #1481081
Posted Monday, August 5, 2013 2:40 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:32 PM
Points: 13,455, Visits: 12,318
Thanks for the ddl and data. What I don't understand is the desired output. Since WKBW has a bit fewer rows let's look at that one.


OUTPUT
WKBW 7/5/13 12:00 PM 7/5/13 2:00 PM
WKBW 7/9/13 11:30 AM 7/9/13 12:30 PM
WKBW 7/12/13 12:30 PM 7/17/13 1:00 PM
WKBW 7/19/13 12:30 PM 7/19/13 2:00 PM


Not sure why your skip the first row 11am - 11:30am and start with the row that begins at noon. Do you only want groups where there are least 2 consecutive times or am I missing something?

Also, why do you have one row that starts on 7/12 but end on 7/17? There is nothing in the data to suggest that. What about the 18th?





_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1481089
Posted Monday, August 5, 2013 2:52 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, June 27, 2014 11:46 AM
Points: 7, Visits: 143
My bad on the copied output data. (I was copying from a slug of events and just manually copying start/end times for my output. The output for WKBW on 7/12 should be: WKBW 7/12/13 12:30 PM 7/12/13 2:00 PM.

Yes, I am only wanting events which have a consecutive events, and grouping them by all of the events in that consecutive string. So any "individual" events (having no consecutive event before or after) would be ignored. If it would be easier to include them, if there is a count of events in the group then I can ignore the "groups" where there is just one event.

Clear as mud now? :)
Post #1481097
Posted Tuesday, August 6, 2013 2:35 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: 2 days ago @ 7:45 AM
Points: 1,694, Visits: 19,552
Try this

WITH Starts AS (
SELECT a.StationID,a.dtmStart,
ROW_NUMBER() OVER(PARTITION BY a.StationID ORDER BY a.dtmStart) AS rn
FROM @Events a
WHERE NOT EXISTS(SELECT * FROM @Events b
WHERE b.StationID = a.StationID
AND b.dtmEnd = a.dtmStart)),
Ends AS (
SELECT a.StationID,a.dtmEnd,
ROW_NUMBER() OVER(PARTITION BY a.StationID ORDER BY a.dtmEnd) AS rn
FROM @Events a
WHERE NOT EXISTS(SELECT * FROM @Events b
WHERE b.StationID=a.StationID
AND b.dtmStart = a.dtmEnd))
SELECT s.StationID,s.dtmStart,e.dtmEnd
FROM Starts s
INNER JOIN Ends e ON e.StationID=s.StationID
AND e.rn=s.rn
INNER JOIN @Events t ON t.StationID=s.StationID
AND t.dtmStart BETWEEN s.dtmStart AND e.dtmEnd
GROUP BY s.StationID,s.dtmStart,e.dtmEnd
HAVING COUNT(*) > 1
ORDER BY s.StationID,s.dtmStart,e.dtmEnd;



____________________________________________________

How to get the best help on a forum

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

Never approach a goat from the front, a horse from the rear, or a fool from any direction.
Post #1481202
Posted Tuesday, August 6, 2013 10:15 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, June 27, 2014 11:46 AM
Points: 7, Visits: 143
Mark, IT WORKS!!!! I never would have figured that out and now I've learned a new "trick" on how to query data. Now I just have to add all of the other columns to it and then duplicate it again so I can add an additional clause to the group by.

Thank you so much for taking the time to help me figure this out.
Post #1481428
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse