473,609 Members | 2,103 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1577
> 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_locatio n VARCHAR(25) NOT NULL,
meeting_start_t ime DATETIME NOT NULL,
meeting_end_tim e DATETIME NOT NULL,
CHECK (meeting_start_ time < meeting_end_tim e),
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_s tart_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****@sommarsk og.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(ti me)
from
(select recordid,Monday start as time
from table where not isnull(mondayst art)
union
select recordid,Tuesda ystart as time
from table where not isnull(Tuesdays tart)
union
select recordid,Wednes daystart as time
from table where not isnull(Wednesda ystart)
union
select recordid,Thursd aystart as time
from table where not isnull(Thursday start)
union
select recordid,Friday start as time
from table where not isnull(Fridayst art)
union
select recordid,Saturd aystart as time
from table where not isnull(Saturday start)
union
select recordid,Sunday start as time
from table where not isnull(Sundayst art)
union
select recordid,null as time
from table where isnull(mondayst art) and isnull(tuesdays tart) and
isnull(wednesda ystart) and isnull(thursday start) and
isnull(fridayst art) and isnull(saturday start) and isnull(sundayst art)
) 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
2966
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 attempting to perform a "left join" query to build a cross-reference table. The left join query is currently taking nearly 2 hours for MySQL to process, using Navicat as a front-end. My system specs are 1.4Mhz Pentium Processor with 1GB of RAM...
5
1545
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 first of each month. Each record has an open date and a close date or the close date is null i.e., the record is not yet closed. I've previously beaten this by building a table, simply a list of the dates for the first of each month for the next...
4
6528
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 heading will attract the knowledgeable forum contributors I'm seeking. Thank-you... I'm being academically challenged trying to build a pass-through query inside of Access 2.0 to "reach out 'n touch" a remote data file running on a Linux box in...
8
5650
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 tables and use a wildcard to create a query for any table meeting that criteria? I have over 100 tables and when I do updates, I've been having to create append queries so that my clients don't loose data, but I would like to be able to have code...
6
4836
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 SalesManName AT Alan Time
1
1810
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, Tags.ParentTagId WHERE Tags.Id IN ( 22536, 22535 )
1
2494
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 fields: ID, First_Name, Last_Name, Org, Email, Status Only "Email" is mandatory, ID is autonumber, the rest are optional. I have to create a query that will allow users to search the table with any of the fields above. A user may search with...
28
2407
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 assistance. We had some assistance in creating an initial query which looked as follows: SELECT equipment.name , equipment.new_modelno, equipment.new_serialno , equipment.new_type ,
3
2444
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 field to the "Contracts" table called "ContractYear". SELECT Contracts.ID, Contracts.Commodity, Contracts.Date, Contracts.ContractYear, Contracts.Option, Contracts., Contracts.Bushels, Contracts.Price, Contracts. FROM Customer INNER JOIN Contracts...
0
8109
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8035
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8534
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8188
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8374
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6969
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4059
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2502
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1630
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.