Here is sample data I'm working with:
ID ShiftDate SegTime
99 5/2/2005 5/2/2005 1:00:00 PM
99 5/2/2005 5/2/2005 1:04:00 PM
99 5/2/2005 5/2/2005 1:43:00 PM
99 5/2/2005 5/2/2005 2:15:00 PM
99 5/2/2005 5/2/2005 2:30:00 PM
99 5/2/2005 5/2/2005 5:00:00 PM
99 5/3/2005 5/3/2005 1:00:00 PM
99 5/3/2005 5/3/2005 1:05:00 PM
99 5/3/2005 5/3/2005 2:15:00 PM
99 5/3/2005 5/3/2005 2:30:00 PM
99 5/3/2005 5/3/2005 5:00:00 PM
Here is the result I'm trying to accomplish:
ID ShiftDate Segstart SegStop
99 5/2/2005 5/2/2005 1:00:00 PM 5/2/2005 1:04:00 PM
99 5/2/2005 5/2/2005 1:04:00 PM 5/2/2005 1:43:00 PM
99 5/2/2005 5/2/2005 1:43:00 PM 5/2/2005 2:15:00 PM
99 5/2/2005 5/2/2005 2:15:00 PM 5/2/2005 2:30:00 PM
99 5/2/2005 5/2/2005 2:30:00 PM 5/2/2005 5:00:00 PM
99 5/3/2005 5/3/2005 1:00:00 PM 5/3/2005 1:05:00 PM
99 5/3/2005 5/3/2005 1:05:00 PM 5/3/2005 2:15:00 PM
99 5/3/2005 5/3/2005 2:15:00 PM 5/3/2005 2:30:00 PM
99 5/3/2005 5/3/2005 2:30:00 PM 5/3/2005 5:00:00 PM
So each record represents a start time and the next record is the end
time. Then that end time becomes the start time of the next record and
so on. Then it starts over with the next shift date. The number of
records vary with each shift date.
Any help building this SQL query will be appreciated. Thanks. 7 1420
although there is a pure SQL solution, solving such problems is a snap
using OLAP aka analytical functions. On SQL 2000 they are not
available, but there is a substitute
---- set up data
create table t(n int, d smalldatetime)
insert into t values(1, '1/1/2005')
insert into t values(1, '1/2/2005')
insert into t values(1, '1/9/2005')
insert into t values(1, '1/14/2005')
insert into t values(2, '1/21/2005')
insert into t values(2, '1/23/2005')
go
create function dbo.t_with_rn()
returns @a table(n int, d smalldatetime, rn int)
as
begin
declare a_cur cursor
local
for select n,d from t order by n,d
declare @n int
declare @d smalldatetime
declare @rn int
set @rn=0
open a_cur
fetch next from a_cur into @n, @d
while @@fetch_status=0
begin
set @rn = @rn + 1
insert into @a values(@n, @d, @rn)
fetch next from a_cur into @n, @d
end
return
end
go
select * from dbo.t_with_rn()
---------this function will assign consecutive numbers to rows
n d rn
----------- ------------------------------------------------------
-----------
1 2005-01-01 00:00:00 1
1 2005-01-02 00:00:00 2
1 2005-01-09 00:00:00 3
1 2005-01-14 00:00:00 4
2 2005-01-21 00:00:00 5
2 2005-01-23 00:00:00 6
go
select left_ends.n, left_ends.d left_end, right_ends.d right_end from
dbo.t_with_rn() left_ends,
dbo.t_with_rn() right_ends
where left_ends.n = right_ends.n
and left_ends.rn = right_ends.rn - 1
go
---------- here are the intervals
n left_end
right_end
----------- ------------------------------------------------------
------------------------------------------------------
1 2005-01-01 00:00:00
2005-01-02 00:00:00
1 2005-01-02 00:00:00
2005-01-09 00:00:00
1 2005-01-09 00:00:00
2005-01-14 00:00:00
2 2005-01-21 00:00:00
2005-01-23 00:00:00
---- clean up
drop function dbo.t_with_rn
go
select * from t
drop table t
(an******@comcast.net) writes: So each record represents a start time and the next record is the end time. Then that end time becomes the start time of the next record and so on. Then it starts over with the next shift date. The number of records vary with each shift date.
Any help building this SQL query will be appreciated. Thanks.
Query below. One thing seems funny to me, and that is I would expect
shifts to last past midnight. But in such case you need find another
criteria for when a row is not the start of a new shift as well.
create table t(id int not null,
shiftdate datetime not null,
shifttime datetime NOT NULL)
insert t(id, shiftdate, shifttime)
SELECT 99, '5/2/2005', '5/2/2005 1:00:00 PM' UNION ALL
SELECT 99, '5/2/2005', '5/2/2005 1:04:00 PM' UNION ALL
SELECT 99, '5/2/2005', '5/2/2005 1:43:00 PM' UNION ALL
SELECT 99, '5/2/2005', '5/2/2005 2:15:00 PM' UNION ALL
SELECT 99, '5/2/2005', '5/2/2005 2:30:00 PM' UNION ALL
SELECT 99, '5/2/2005' , '5/2/2005 5:00:00 PM' UNION ALL
SELECT 99, '5/3/2005', '5/3/2005 1:00:00 PM' UNION ALL
SELECT 99, '5/3/2005', '5/3/2005 1:05:00 PM' UNION ALL
SELECT 99, '5/3/2005', '5/3/2005 2:15:00 PM' UNION ALL
SELECT 99, '5/3/2005', '5/3/2005 2:30:00 PM' UNION ALL
SELECT 99, '5/3/2005', '5/3/2005 5:00:00 PM'
go
SELECT id, shifttime, shiftend
FROM (SELECT t.id, t.shifttime,
shiftend = (SELECT MIN(t2.shifttime)
FROM t t2
WHERE t2.id = t.id
AND t2.shiftdate = t.shiftdate
AND t2.shifttime > t.shifttime)
FROM t) AS x
WHERE shiftend IS NOT NULL
ORDER BY id, shifttime
go
drop table t
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se
Books Online for SQL Server SP3 at http://www.microsoft.com/sql/techinf...2000/books.asp
Erland,
There are shifts that end past midnight. But the shiftdate always
represents the date the shift started. In the original sample data, ID
99 represents an employee. ShiftDate 5/2/2005 represents the entire
work day. The segtimes are start/end times of different segments of
the day such as taking cust calls, training, break, lunch, etc. So Emp
99 worked from 1p-5p on 5/2. She could have very well ended her shift
past midnight, but the shift date would have been the same.
AK,
Thanks for the response. I am new to SQL and your code was a little
over my head but I tried to work with it. I ended up creating 3 stored
procedures and one custom function, but I don't know how to call the
procedures to get the data. Did I do it right and what else do I need
to do?
Thanks.
AK wrote: although there is a pure SQL solution, solving such problems is a snap using OLAP aka analytical functions. On SQL 2000 they are not available, but there is a substitute
---- set up data create table t(n int, d smalldatetime) insert into t values(1, '1/1/2005') insert into t values(1, '1/2/2005') insert into t values(1, '1/9/2005') insert into t values(1, '1/14/2005') insert into t values(2, '1/21/2005') insert into t values(2, '1/23/2005') go create function dbo.t_with_rn() returns @a table(n int, d smalldatetime, rn int) as begin declare a_cur cursor local for select n,d from t order by n,d
declare @n int declare @d smalldatetime declare @rn int
set @rn=0
open a_cur
fetch next from a_cur into @n, @d
while @@fetch_status=0 begin set @rn = @rn + 1
insert into @a values(@n, @d, @rn)
fetch next from a_cur into @n, @d end return end go select * from dbo.t_with_rn() ---------this function will assign consecutive numbers to rows n d rn
----------- ------------------------------------------------------ ----------- 1 2005-01-01 00:00:00 1 1 2005-01-02 00:00:00 2 1 2005-01-09 00:00:00 3 1 2005-01-14 00:00:00 4 2 2005-01-21 00:00:00 5 2 2005-01-23 00:00:00 6
go select left_ends.n, left_ends.d left_end, right_ends.d right_end from dbo.t_with_rn() left_ends, dbo.t_with_rn() right_ends where left_ends.n = right_ends.n and left_ends.rn = right_ends.rn - 1 go ---------- here are the intervals n left_end right_end ----------- ------------------------------------------------------ ------------------------------------------------------ 1 2005-01-01 00:00:00 2005-01-02 00:00:00 1 2005-01-02 00:00:00 2005-01-09 00:00:00 1 2005-01-09 00:00:00 2005-01-14 00:00:00 2 2005-01-21 00:00:00 2005-01-23 00:00:00
---- clean up drop function dbo.t_with_rn go select * from t drop table t
AK,
Thanks for the response. I am new to SQL and your code was a little
over my head but I tried to work with it. I ended up creating 3 stored
procedures and one custom function, but I don't know how to call the
procedures to get the data. Did I do it right and what else do I need
to do?
Thanks.
AK wrote: although there is a pure SQL solution, solving such problems is a snap using OLAP aka analytical functions. On SQL 2000 they are not available, but there is a substitute
---- set up data create table t(n int, d smalldatetime) insert into t values(1, '1/1/2005') insert into t values(1, '1/2/2005') insert into t values(1, '1/9/2005') insert into t values(1, '1/14/2005') insert into t values(2, '1/21/2005') insert into t values(2, '1/23/2005') go create function dbo.t_with_rn() returns @a table(n int, d smalldatetime, rn int) as begin declare a_cur cursor local for select n,d from t order by n,d
declare @n int declare @d smalldatetime declare @rn int
set @rn=0
open a_cur
fetch next from a_cur into @n, @d
while @@fetch_status=0 begin set @rn = @rn + 1
insert into @a values(@n, @d, @rn)
fetch next from a_cur into @n, @d end return end go select * from dbo.t_with_rn() ---------this function will assign consecutive numbers to rows n d rn
----------- ------------------------------------------------------ ----------- 1 2005-01-01 00:00:00 1 1 2005-01-02 00:00:00 2 1 2005-01-09 00:00:00 3 1 2005-01-14 00:00:00 4 2 2005-01-21 00:00:00 5 2 2005-01-23 00:00:00 6
go select left_ends.n, left_ends.d left_end, right_ends.d right_end from dbo.t_with_rn() left_ends, dbo.t_with_rn() right_ends where left_ends.n = right_ends.n and left_ends.rn = right_ends.rn - 1 go ---------- here are the intervals n left_end right_end ----------- ------------------------------------------------------ ------------------------------------------------------ 1 2005-01-01 00:00:00 2005-01-02 00:00:00 1 2005-01-02 00:00:00 2005-01-09 00:00:00 1 2005-01-09 00:00:00 2005-01-14 00:00:00 2 2005-01-21 00:00:00 2005-01-23 00:00:00
---- clean up drop function dbo.t_with_rn go select * from t drop table t
(an******@comcast.net) writes: There are shifts that end past midnight. But the shiftdate always represents the date the shift started. In the original sample data, ID 99 represents an employee. ShiftDate 5/2/2005 represents the entire work day. The segtimes are start/end times of different segments of the day such as taking cust calls, training, break, lunch, etc. So Emp 99 worked from 1p-5p on 5/2. She could have very well ended her shift past midnight, but the shift date would have been the same.
OK. In that case the query would work. I was thinking that having two
columns were redudant, since the date appears in both, but with your
explanation, I see why the separate date column is needed.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se
Books Online for SQL Server SP3 at http://www.microsoft.com/sql/techinf...2000/books.asp
Angel,
I would use Erland's solution - it is way simpler and more practical This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: lawrence |
last post by:
I've been bad about documentation so far but I'm going to try to be
better. I've mostly worked alone so I'm the only one, so far, who's
suffered from my bad habits. But I'd like other programmers...
|
by: netpurpose |
last post by:
I need to extract data from this table to find the lowest prices of
each product as of today. The product will be listed/grouped by the
name only, discarding the product code - I use...
|
by: paii |
last post by:
I have a table that stores job milestone dates. The 2 milestones I am
interested in are "Ship Date" TypeID 1 and "Revised Ship Date" TypeID 18.
All jobs have TypeID 1 only some jobs have TypeID 18....
|
by: pw |
last post by:
Hi,
I am having a mental block trying to figure out how to code this.
Two tables:
"tblQuestions" (fields = quesnum, questype, question)
"tblAnswers" (fields = clientnum, quesnum, questype,...
|
by: K. Crothers |
last post by:
I administer a mechanical engineering database. I need to build a
query which uses the results from a subquery as its input or
criterion.
I am attempting to find all of the component parts of...
|
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...
|
by: ward |
last post by:
Greetings.
Ok, I admit it, I bit off a bit more than I can chew.
I need to complete this "Generate Report" page for my employer and I'm
a little over my head. I could use some additional...
|
by: L. R. Du Broff |
last post by:
I own a small business. Need to track a few hundred pieces of rental
equipment that can be in any of a few dozen locations. I'm an old-time C
language programmer (UNIX environment). If the only...
|
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...
|
by: pbd22 |
last post by:
Hi.
I need some help with structuring my query strings.
I have a form with a search bar and some links. Each
link is a search type (such as "community"). The HREF
for the link's anchor looks...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
| |