I've been scratching my head on this for quite awhile and it has me stumped.
I hope to define a query which I can use to fill a "day planner" type of
calendar. Although I've see a lot of these, only one has had what I think
is a really nice feature - it collected into groups all events that
overlapped into contiguous blocks of time. The net result of this is that
it becomes possible to output a calendar (html table) that is much less
cluttered. So I want to use this same idea for my own little project.
The trick is that events may (or may not) start and end such that they
overlap (completely or only at one end). I only am concerned with events on
a given day.
My "events" table contains eventtitle, date,starthour and endhour. Hours
are numbered from "0" to "23". I also have a lookup table of the "hours of
the day" with which I did a JOIN to include the "missing" hours (where there
were no events - making a query that returned events for each hour and nulls
for each hour that had no event scheduled for it. But this makes too many
"blank" rows, which is part of the clutter to which I referred.
I've been able to construct queries that work in some cases, but not all.
I've reread my copy of Celko's SQL For Smarties and came close, but no
cigar. Where he discusses hotels and room-nights is part of the solution I
needed, but my need goes beyond that quite a bit.
Basically, I need to calculate one or more "spans" that contain contiguous
groups of start/end times. By knowing the number of hours spanned, I can
then use that for a <TD rowspan='n'> to collect my data like I want.
I seem to keep hitting all around the solution. Maybe there isn't one (that
is purely a SQL solution). Or maybe I'm just looking at the problem the
wrong way.
So I thought I'd see if anyone here might point me in the right (or at least
"new") direction. I've been looking at this for so long, I'm probably
overlooking some simple and obvious trick to do this. Or maybe I'll get
lucky and someone has seen or done exactly this already and can provide a
solution?
Incidentally, I've avoided utilizing a stored procedure or making a number
of temp tables to collect intermediate results, as I might need to port this
to a "dumb" database that does not provide such facilities. Maybe that's
impractical?
thanks in advance,
--