473,385 Members | 1,312 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,385 software developers and data experts.

Need help creating query

I am working with employee schedules. Each schedule is comprised of
segments (shift, lunch, break, training, etc.) that have rankings.
Each record has the employee id, the date the shift starts, the start
and end time of each segment, the duration,the segment type and its
rank. The start and end times of the schedules can overlap, but the
segment that has the higher rank takes precedence.

As a simple example, an employee working 8a-5p will have two records.
The shift segment from 8a-5p and a lunch segment from 12p-1p. The
lunch ranks higher so, even though the shift goes from 8a-5p, from
12p-1p, the lunch takes precedence.

What I'm trying to do is build a query that will collapse the segments.
So given the example above, I will now have three records: shift
8a-12p, lunch 12p-1p, shift 1p-5p.

I have been racking my brain but I just can't figure it out.
Ultimately, a programmer in my office will display this in a bar chart
format with each segment type having its own color. I'm trying to do as
much of the work in SQL so he doesn't have to calculate this stuff in
the code.

Any help would be appreciated.

Thanks.
Angela

Jul 23 '05 #1
5 1676
Hi

Posting DDL and example data removes any ambiguity that a long description
may contain. http://www.aspfaq.com/etiquett*e.asp?id=5006

It sounds like you have a repeating groups in your records say

CREATE TABLE Schedules ( employeeid int not null,
shiftstartdate datetime not null,
s1start datetime,
s1end datetime,
s1type int,
s1rank int,
s2start datetime,
s2end datetime,
s2type int,
s2rank int,
s3start datetime,
s3end datetime,
s3type int,
s3rank int )

As rank is an attribute of the segment type it may not be necessary to store
this as type can be looked up.

The to get separate records you could try something like:

SELECT employeeid,
shiftstartdate,
s1start, AS SegmentStart,
s1end, AS SegmentEnd
s1type AS SegmentType,
s1rank AS SegmentRank
UNION ALL
SELECT employeeid,
shiftstartdate,
s2start,
s2end,
s2type,
s2rank
WHERE s2start IS NOT NULL
UNION ALL
SELECT employeeid,
shiftstartdate,
s3start,
s3end,
s3type,
s3rank
WHERE s3start IS NOT NULL
ORDER BY SegmentRank

John

<an******@comcast.net> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
I am working with employee schedules. Each schedule is comprised of
segments (shift, lunch, break, training, etc.) that have rankings.
Each record has the employee id, the date the shift starts, the start
and end time of each segment, the duration,the segment type and its
rank. The start and end times of the schedules can overlap, but the
segment that has the higher rank takes precedence.

As a simple example, an employee working 8a-5p will have two records.
The shift segment from 8a-5p and a lunch segment from 12p-1p. The
lunch ranks higher so, even though the shift goes from 8a-5p, from
12p-1p, the lunch takes precedence.

What I'm trying to do is build a query that will collapse the segments.
So given the example above, I will now have three records: shift
8a-12p, lunch 12p-1p, shift 1p-5p.

I have been racking my brain but I just can't figure it out.
Ultimately, a programmer in my office will display this in a bar chart
format with each segment type having its own color. I'm trying to do as
much of the work in SQL so he doesn't have to calculate this stuff in
the code.

Any help would be appreciated.

Thanks.
Angela

Jul 23 '05 #2
Here is the table structure. I did not create this table. The data
was exported from our workforce management software.

PRI_INDEX Long Integer,
EMP_ID Long Integer,
SHIFTDATE Date/Time,
SEG_CODE Text,
START_MOMENT Date/Time,
STOP_MOMENT Date/Time,
DURATION Long Integer,
RANK Long Integer,

Here is sample data of an employee who works 8a-5p on a given day:

111, 666, 6/1/2005, LUNCH, 6/1/2005 12:00:00 PM, 6/1/2005 1:00:00 PM,
60, 1
112, 666, 6/1/2005, SHIFT, 6/1/2005 8:00:00 AM, 6/1/2005 5:00:00 PM,
540, 2

Here is the result I would like:

666, 6/1/2005, SHIFT, 6/1/2005 8:00:00 AM, 6/1/2005 12:00:00 PM, 240, 2
666, 6/1/2005, LUNCH, 6/1/2005 12:00:00 PM, 6/1/2005 1:00:00 PM, 60, 1
666, 6/1/2005, SHIFT, 6/1/2005 1:00:00 PM, 6/1/2005 5:00:00 PM, 240, 2

Thanks again.

Jul 23 '05 #3
Hi

This may be overly simplified but, if there is always the pair of records:

SELECT S.EMP_ID,
S.SHIFTDATE,
S.SEG_CODE,
S.START_MOMENT,
L.START_MOMENT AS STOP_MOMENT,
DATEDIFF ( MI, S.START_MOMENT, L.START_MOMENT ) AS DURATION,
S.RANK
FROM SCHEDULES S
JOIN SCHEDULES L ON S.EMP_ID = L.EMP_ID
AND S.SHIFTDATE = L.SHIFTDATE
AND S.SEG_CODE = 'SHIFT'
AND L.SEG_CODE = 'LUNCH'
UNION
SELECT L.EMP_ID,
L.SHIFTDATE,
L.SEG_CODE,
L.START_MOMENT,
L.STOP_MOMENT,
L.DURATION,
L.RANK
FROM SCHEDULES L
WHERE L.SEG_CODE = 'LUNCH'
UNION ALL SELECT S.EMP_ID,
S.SHIFTDATE,
S.SEG_CODE,
L.STOP_MOMENT AS START_MOMENT,
S.STOP_MOMENT,
DATEDIFF ( MI, L.STOP_MOMENT, S.STOP_MOMENT ) AS DURATION,
S.RANK
FROM SCHEDULES S
JOIN SCHEDULES L ON S.EMP_ID = L.EMP_ID
AND S.SHIFTDATE = L.SHIFTDATE
AND S.SEG_CODE = 'SHIFT'
AND L.SEG_CODE = 'LUNCH'
ORDER BY S.EMP_ID, S.START_MOMENT

John
<an******@comcast.net> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Here is the table structure. I did not create this table. The data
was exported from our workforce management software.

PRI_INDEX Long Integer,
EMP_ID Long Integer,
SHIFTDATE Date/Time,
SEG_CODE Text,
START_MOMENT Date/Time,
STOP_MOMENT Date/Time,
DURATION Long Integer,
RANK Long Integer,

Here is sample data of an employee who works 8a-5p on a given day:

111, 666, 6/1/2005, LUNCH, 6/1/2005 12:00:00 PM, 6/1/2005 1:00:00 PM,
60, 1
112, 666, 6/1/2005, SHIFT, 6/1/2005 8:00:00 AM, 6/1/2005 5:00:00 PM,
540, 2

Here is the result I would like:

666, 6/1/2005, SHIFT, 6/1/2005 8:00:00 AM, 6/1/2005 12:00:00 PM, 240, 2
666, 6/1/2005, LUNCH, 6/1/2005 12:00:00 PM, 6/1/2005 1:00:00 PM, 60, 1
666, 6/1/2005, SHIFT, 6/1/2005 1:00:00 PM, 6/1/2005 5:00:00 PM, 240, 2

Thanks again.

Jul 23 '05 #4
What I gave was a very simplified example. In actuality you will have
something like this:

111, 666, 6/1/2005, LUNCH, 6/1/2005 12:00:00 PM, 6/1/2005 1:00:00 PM,
60, 1
111, 666, 6/1/2005, BREAK1, 6/1/2005 10:00:00 AM, 6/1/2005 10:15:00
AM,15, 2
111, 666, 6/1/2005, BREAK2, 6/1/2005 3:00:00 PM, 6/1/2005 3:15:00 PM,
15, 3
111, 666, 6/1/2005, TRAIN, 6/1/2005 8:30:00 AM, 6/1/2005 9:00:00 AM,
30, 4
112, 666, 6/1/2005, SHIFT, 6/1/2005 8:00:00 AM, 6/1/2005 5:00:00 PM,
540, 5
Then the result would be:
666, 6/1/2005, SHIFT, 6/1/2005 8:00:00 AM, 6/1/2005 8:30:00 AM, 30, 5
666, 6/1/2005, TRAIN, 6/1/2005 8:30:00 AM, 6/1/2005 9:00:00 AM, 30, 4
666, 6/1/2005, SHIFT, 6/1/2005 9:00:00 AM, 6/1/2005 10:00:00 AM, 60, 5
666, 6/1/2005, BREAK1, 6/1/2005 10:00:00 AM, 6/1/2005 10:15:00 AM, 15,
2
666, 6/1/2005, SHIFT, 6/1/2005 10:15:00 AM, 6/1/2005 12:00:00 PM, 105,
5
666, 6/1/2005, LUNCH, 6/1/2005 12:00:00 PM, 6/1/2005 1:00:00 PM, 60, 1
666, 6/1/2005, SHIFT, 6/1/2005 1:00:00 PM, 6/1/2005 3:00:00 PM, 120, 5
666, 6/1/2005, BREAK2, 6/1/2005 3:00:00 PM, 6/1/2005 3:15:00 PM, 15, 3
666, 6/1/2005, SHIFT, 6/1/2005 3:15:00 PM, 6/1/2005 5:00:00 PM, 105, 5

Will the query still work?

Thanks.

Jul 23 '05 #5
Hi

The query relies on one lunch per shift, what you will need to do is create
a more generic solution using the end time of a non-shift activity as the
start time and the earliest start time of a non-shift activity as the end
time . This will be unioned to the start of shift and the earliest non-shift
start time as the end time and also unioned with the latest non-shift end
time as the start time and the end of shift.

John

<an******@comcast.net> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
What I gave was a very simplified example. In actuality you will have
something like this:

111, 666, 6/1/2005, LUNCH, 6/1/2005 12:00:00 PM, 6/1/2005 1:00:00 PM,
60, 1
111, 666, 6/1/2005, BREAK1, 6/1/2005 10:00:00 AM, 6/1/2005 10:15:00
AM,15, 2
111, 666, 6/1/2005, BREAK2, 6/1/2005 3:00:00 PM, 6/1/2005 3:15:00 PM,
15, 3
111, 666, 6/1/2005, TRAIN, 6/1/2005 8:30:00 AM, 6/1/2005 9:00:00 AM,
30, 4
112, 666, 6/1/2005, SHIFT, 6/1/2005 8:00:00 AM, 6/1/2005 5:00:00 PM,
540, 5
Then the result would be:
666, 6/1/2005, SHIFT, 6/1/2005 8:00:00 AM, 6/1/2005 8:30:00 AM, 30, 5
666, 6/1/2005, TRAIN, 6/1/2005 8:30:00 AM, 6/1/2005 9:00:00 AM, 30, 4
666, 6/1/2005, SHIFT, 6/1/2005 9:00:00 AM, 6/1/2005 10:00:00 AM, 60, 5
666, 6/1/2005, BREAK1, 6/1/2005 10:00:00 AM, 6/1/2005 10:15:00 AM, 15,
2
666, 6/1/2005, SHIFT, 6/1/2005 10:15:00 AM, 6/1/2005 12:00:00 PM, 105,
5
666, 6/1/2005, LUNCH, 6/1/2005 12:00:00 PM, 6/1/2005 1:00:00 PM, 60, 1
666, 6/1/2005, SHIFT, 6/1/2005 1:00:00 PM, 6/1/2005 3:00:00 PM, 120, 5
666, 6/1/2005, BREAK2, 6/1/2005 3:00:00 PM, 6/1/2005 3:15:00 PM, 15, 3
666, 6/1/2005, SHIFT, 6/1/2005 3:15:00 PM, 6/1/2005 5:00:00 PM, 105, 5

Will the query still work?

Thanks.

Jul 23 '05 #6

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

Similar topics

9
by: sk | last post by:
I have an applicaton in which I collect data for different parameters for a set of devices. The data are entered into a single table, each set of name, value pairs time-stamped and associated with...
3
by: CSDunn | last post by:
Hello, I have 14 fields on a report that hold integer values. The field names use the following naming convention: T1Number, T2Number ....T14Number. I need to get a 'sub total' of all fields as...
1
by: longtim | last post by:
I have been having endless difficulty creating reports/queries that set any relevent parameters from controls in forms. I am creating an application under access 2003 but will target access...
1
by: Grammie | last post by:
I need a report that looks like this: 1. Number of Enrollees as of Today: (count of everyone in query){can count last names) 2. Total Active People (count of everyone with "A" as...
3
by: google | last post by:
I have a database with four table. In one of the tables, I use about five lookup fields to get populate their dropdown list. I have read that lookup fields are really bad and may cause problems...
2
by: deja | last post by:
Hello, I am creating an a to z list - basically a count of all results that start with the letter "A", "B", "C" .... and so on. I am pretty poor at SQL so I am sure some brains out there can...
8
by: pamelafluente | last post by:
I am beginning aspNet, I know well win apps. Need a simple and schematic code example to start work. This is what I need to accomplish: ---------------------- Given button and a TextBox on a...
5
by: darnnews | last post by:
Hi, I have been creating a database to keep track of press clippings, but I have hit a couple stumbling blocks. Any help is much appreciate. 1) Seeing if my query is done I have the...
7
by: Rnykster | last post by:
I know a little about Access and have made several single table databases. Been struggling for about a month to do a multiple table database with no success. Help! There are two tables. First...
2
by: Jim Devenish | last post by:
I have a table named StockItems with field: StockItemID QuantityInStock I am creating assemblies of stock items and have another table named StockItemAssemblies with fields:...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.