Welcome to dbForumz.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Creative query assistance needed

 
   Database Forums (Home) -> General Discussions RSS
Next:  SQL 2000: Transport-level error on SELECT stateme..  
Author Message
t8ntboy

External


Since: May 04, 2007
Posts: 12



(Msg. 1) Posted: Thu Aug 14, 2008 11:15 am
Post subject: Creative query assistance needed
Archived from groups: comp>databases>ms-sqlserver (more info?)

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.

 >> Stay informed about: Creative query assistance needed 
Back to top
Login to vote
--CELKO--

External


Since: Apr 17, 2007
Posts: 417



(Msg. 2) Posted: Thu Aug 14, 2008 12:13 pm
Post subject: Re: Creative query assistance needed [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

>> I have a table that contains a field [sic: columns are not fields] for the start time for each day of the week (e.g., MondayStart, TuesdayStart, WednesdayStart, FridayStart, SaturdayStart, SundayStart). <<

There is no such thing as "THE week", unless you mean the magazine by
that title. There infinitely many weeks which contain infinitely many
Mondays, etc. You have the wrong mental model of time. Download a
free copy of the Rick Snodgrass book on TEMPORAL QUERIES IN SQL from
the University of Arizona website.

>> I need a query that yields the first start time from any of the days. The problem is that, in many cases/records [sic: rows are not 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 should no start time exist at all for any of the days. <<

Where is the DDL? What is a meeting? Why do they have no durations in
this vague narrative?

The usual approach to this is the have a Calendar table which has all
the temporal data for the enterprise, such as holidays. You easily
create 100 years of data with a spreadsheet in a day or two. Now,
let's fix that missing DDL:

CREATE TABLE MeetingSchedule
(meeting_id INTEGER NOT NULL PRIMARY KEY,
meeting_title VARCHAR(25) NOT NULL,
meeting_location VARCHAR(25) NOT NULL,
meeting_start_time DATETIME NOT NULL,
meeting_end_time DATETIME NOT NULL,
CHECK (meeting_start_time < meeting_end_time),
etc.);

Your calendar table will have julianized weeks (week_nbr) and you will
need to look it up for your queries; it also has the ISO weekday codes
or names.

SELECT @my_week, C.weekday_name, MIN(S.meeting_start_time)
FROM (SELECT cal_date, weekday_name
FROM Calendar
WHERE week_nbr = @my_week_nbr
) AS C
LEFT OUTER JOIN
MeetingSchedule AS S
ON C.cal_date = EXTRACT (DATE FROM S.meeting_start_time)
GROUP BY C.weekday_name;

 >> Stay informed about: Creative query assistance needed 
Back to top
Login to vote
Erland Sommarskog2

External


Since: May 30, 2004
Posts: 1649



(Msg. 3) Posted: Thu Aug 14, 2008 9:47 pm
Post subject: Re: Creative query assistance needed [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

t8ntboy ( ) writes:
> 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?

I'm sorry, but I am completely lost. I can only respond with the
standard suggestion that you post:

o CREATE TABLE statements for your table(s).
o INSERT statements with sample data.
o The desired result from the sample.
o Which version of SQL Server you are using.

This makes it simple to copy and paste into query tool to develop a
tested solution. It also helps to clarify your description.


--
Erland Sommarskog, SQL Server MVP, esquel DeleteThis @sommarskog.se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
 >> Stay informed about: Creative query assistance needed 
Back to top
Login to vote
Iain Sharp

External


Since: Feb 22, 2008
Posts: 6



(Msg. 4) Posted: Mon Aug 18, 2008 11:15 am
Post subject: Re: Creative query assistance needed [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Thu, 14 Aug 2008 11:15:45 -0700 (PDT), t8ntboy
wrote:

>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
 >> Stay informed about: Creative query assistance needed 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Query Assistance Needed - Please - Alright, I have this table called Tags. The three columns of interest are Tags.Id, Tags.Name, Tags.ParentTagId This is the query I am currently using: Select Tags.Id, Tags.Name, Tags.ParentTagId >From Tags WHERE Tags.Id IN ( 22536, 22535 ) ...

Query Assistance Needed - Please - Alright, I have this table called Tags. The three columns of interest are Tags.Id, Tags.Name, Tags.ParentTagId This is the query I am currently using: Select Tags.Id, Tags.Name, Tags.ParentTagId >From Tags WHERE Tags.Id IN ( 22536, 22535 ) ...

Query Assistance Needed - Please - Alright, I have this table called Tags. The three columns of interest are Tags.Id, Tags.Name, Tags.ParentTagId This is the query I am currently using: Select Tags.Id, Tags.Name, Tags.ParentTagId >From Tags WHERE Tags.Id IN ( 22536, 22535 ) ...

query performance help needed - Hello, I have a question I created a SP that by using dynamic sql access SP in all databases with certain name and get the output into a temp table and returns it's contents. The stored procedure in other databases is very simple couple of joins and wher...

Help Needed For writting a query (SQl Server 2005). - Hi experts, I am working on SQL Server 2005. Now i have to write a query which will extract some data from 3 to 4 tables. My main table is having few columns supose 4 columns. EmpNo EmpName Date_Of_Joining Date_Of_Appraisal Now records are..
   Database Forums (Home) -> General Discussions All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]