I have a table that looks like the following:
StudyId StudyStartDateTime
============================
1 01/01/2009 08:45 AM
2 01/01/2009 08:53 AM
...
I'd like to return a query that contains the StudyId, as well as the ten-minute interval during which it was started:
StudyId Interval
==================
1 8:40 AM
2 8:50 AM
This is because I'd like to create a graph displaying the distribution of the studies across ten minute intervals.
The intervals are every ten minutes between 8AM-9AM, and 1PM-2PM. I've gotten this far:
SELECT s.studyid,
s.studystartdatetime,
Iif(Format(s.studystartdatetime,'hh:nn:ss AM/PM') BETWEEN "08:00:00 AM" AND "08:09:59 AM",
"8:00 - 8:10",0) AS ["8:00 - 8:10"],
Iif(Format(s.studystartdatetime,'hh:nn:ss AM/PM') BETWEEN "08:10:00 AM" AND "08:19:59 AM",
"8:10 - 8:20",0) AS ["8:10 - 8:20"],
Iif(Format(s.studystartdatetime,'hh:nn:ss AM/PM') BETWEEN "08:20:00 AM" AND "08:29:59 AM",
"8:20 - 8:30",0) AS ["8:20 - 8:30"],
Iif(Format(s.studystartdatetime,'hh:nn:ss AM/PM') BETWEEN "08:30:00 AM" AND "08:39:59 AM",
"8:30 - 8:40",0) AS ["8:30 - 8:40"],
Iif(Format(s.studystartdatetime,'hh:nn:ss AM/PM') BETWEEN "08:40:00 AM" AND "08:49:59 AM",
"8:40 - 8:50",0) AS ["8:40 - 8:50"],
Iif(Format(s.studystartdatetime,'hh:nn:ss AM/PM') BETWEEN "08:50:00 AM" AND "08:59:59 AM",
"8:50 - 9:00",0) AS ["8:50 - 9:00"],
Iif(Format(s.studystartdatetime,'hh:nn:ss AM/PM') BETWEEN "01:00:00 PM" AND "01:09:59 PM",
"1:00 - 1:10",0) AS ["1:00 - 1:10"],
Iif(Format(s.studystartdatetime,'hh:nn:ss AM/PM') BETWEEN "01:10:00 PM" AND "01:19:59 PM",
"1:10 - 1:20",0) AS ["1:10 - 1:20"],
Iif(Format(s.studystartdatetime,'hh:nn:ss AM/PM') BETWEEN "01:20:00 PM" AND "01:29:59 PM",
"1:20 - 1:30",0) AS ["1:20 - 1:30"],
Iif(Format(s.studystartdatetime,'hh:nn:ss AM/PM') BETWEEN "01:30:00 PM" AND "01:39:59 PM",
"1:30 - 1:40",0) AS ["1:30 - 1:40"],
Iif(Format(s.studystartdatetime,'hh:nn:ss AM/PM') BETWEEN "01:40:00 PM" AND "01:49:59 PM",
"1:40 - 1:50",0) AS ["1:40 - 1:50"],
Iif(Format(s.studystartdatetime,'hh:nn:ss AM/PM') BETWEEN "01:50:00 PM" AND "01:59:59 PM",
"1:50 - 2:00",0) AS ["1:50 - 2:00"]
FROM dbo_study_viewx211_rpt AS s
Which results in something like this:
StudyId StudyStartDateTime 8:00 - 8:10 8:10 - 8:20 ...
==================================================================
1 28/11/2007 8:15:22 AM 0 8:10 - 8:20 0 ...
...
How can I group all of the intervals into a single "Interval" column?