On Thu, 14 Aug 2008 11:15:45 -0700 (PDT), t8ntboy <t8ntboy@gmail.com>
wrote:
Quote:
>I have a table that contains a field for the start time for each day
>of the week (e.g., MondayStart, TuesdayStart, WednesdayStart,
>FridayStart, SaturdayStart, SundayStart).
>
>I need a query that yields the first start time from any of the days.
>The problem is that, in many cases/records, there is not start time at
>all, or there are start times on multiple days. Some meetings may only
>be on Monday where others may be on Tuesday and Thursday, or there may
>be no meeting at all. I want the query to find the first start time
>(if one exists) and report it back, or provide a null value should no
>start time exist at all for any of the days.
>
>Ideally, the results would look something like.
>
>
>RecordID Time
>2342 1300
>3452 0900
>3432 null
>5634 8900
>
>
>Does anyone have a creative solution?
>
>Thanks.
Well, one solution (not necessarily the best) would be.
select RecordID,min(time)
from
(select recordid,Mondaystart as time
from table where not isnull(mondaystart)
union
select recordid,Tuesdaystart as time
from table where not isnull(Tuesdaystart)
union
select recordid,Wednesdaystart as time
from table where not isnull(Wednesdaystart)
union
select recordid,Thursdaystart as time
from table where not isnull(Thursdaystart)
union
select recordid,Fridaystart as time
from table where not isnull(Fridaystart)
union
select recordid,Saturdaystart as time
from table where not isnull(Saturdaystart)
union
select recordid,Sundaystart as time
from table where not isnull(Sundaystart)
union
select recordid,null as time
from table where isnull(mondaystart) and isnull(tuesdaystart) and
isnull(wednesdaystart) and isnull(thursdaystart) and
isnull(fridaystart) and isnull(saturdaystart) and isnull(sundaystart)
) as starttimes
This will effectively return a dataset with two columns, recordID and
time, with an entry for each meeting start time, or a null if there is
no meeting start time set. Then it will get the minimum start time
from that set for each record ID.
Not going to be quick though.
Regards
Iain