473,411 Members | 1,991 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

Creative query assistance needed

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.
Aug 14 '08 #1
3 1567
> 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;
Aug 14 '08 #2
t8ntboy (t8*****@gmail.com) 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, es****@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/prodinf...ons/books.mspx

Aug 14 '08 #3
On Thu, 14 Aug 2008 11:15:45 -0700 (PDT), t8ntboy <t8*****@gmail.com>
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
Aug 18 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: DBNovice | last post by:
I have a database that keeps records on the issue and failure of an item. Currently, the database is poorly desisned; therefore I'm performing queries to break the data into normalized tables and...
5
by: Bill | last post by:
Good Day; I would appreciate assistance developing a query that I haven't been able to develop without using a second table. I wish to count the number of records that are still open on the...
4
by: MLH | last post by:
I apologize in advance to forum readers noticing this somewhat redundant post. I fear that my Subject Heading was ill-chosen in earlier post I made on this topic. Am hoping that a clearer Subject...
8
by: Lumpierbritches | last post by:
Thank you in advance for any and all assistance, it is GREATLY appreciated. I was wondering if there is a way, with code to create a query for every table? Or can you use a naming convention in...
6
by: jjturon | last post by:
Can anyone help me?? I am trying to pass a Select Query variable to a table using Dlookup and return the value to same select query but to another field. Ex. SalesManID ...
1
by: Andrew Tatum | last post by:
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,...
1
by: gautam89 | last post by:
Hi everyone... I'm a high school student working on an Access project for a summer internship. I needed your assistance in writing a criteria for a select query. Table1 has the following...
28
by: Mark Braithwaite | last post by:
Hi We are attempting to create a dynamic spreadsheet which will extract information from CRM / SQL. We have very limited expierence in creating SQL queries and we would really appreciate any...
3
by: hrreece | last post by:
I am trying to use the Dlookup expression in a macro to pull the value from the "Price" field in the following query. The query works fine and the Dlookup function did as well until I added a new...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.