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

Rolling up spans without breaks between them

I'm trying to figure out how to this do with TSQL and preferably
through joins rather than by using cursors or temp tables. I have
multiple time spans for multiple individuals that I would like to
rollup together where they don't actually have a gap in coverage.

ID StartDate EndDate
Z000001 01/01/1996 9/30/1996
Z000001 10/01/1996 12/31/1998
Z000001 01/01/1999 04/30/1999
Z000001 01/01/2000 12/31/2000
Z000001 01/01/2001 12/31/2001

I would like to be able to "roll these up" in order to reflect the
actual time frames so that it would like this:

ID StartDate EndDate
Z000001 01/01/1996 4/30/1999
Z000001 01/01/2000 12/31/2001

4/30/1999 to 01/01/2000 is, of course, a legitimate break where there
was a date difference of more than one day.

Thanks in advance for any ideas.

Nov 10 '06 #1
7 1670
(da******@aol.com) writes:
I'm trying to figure out how to this do with TSQL and preferably
through joins rather than by using cursors or temp tables. I have
multiple time spans for multiple individuals that I would like to
rollup together where they don't actually have a gap in coverage.

ID StartDate EndDate
Z000001 01/01/1996 9/30/1996
Z000001 10/01/1996 12/31/1998
Z000001 01/01/1999 04/30/1999
Z000001 01/01/2000 12/31/2000
Z000001 01/01/2001 12/31/2001

I would like to be able to "roll these up" in order to reflect the
actual time frames so that it would like this:

ID StartDate EndDate
Z000001 01/01/1996 4/30/1999
Z000001 01/01/2000 12/31/2001

4/30/1999 to 01/01/2000 is, of course, a legitimate break where there
was a date difference of more than one day.
This appears to work, but you should test it with more dates. Note
that I am allowing the intervals to overlap.

It may not be that very efficient.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Nov 10 '06 #2
Erland Sommarskog wrote:
(da******@aol.com) writes:
I'm trying to figure out how to this do with TSQL and preferably
through joins rather than by using cursors or temp tables. I have
multiple time spans for multiple individuals that I would like to
rollup together where they don't actually have a gap in coverage.

ID StartDate EndDate
Z000001 01/01/1996 9/30/1996
Z000001 10/01/1996 12/31/1998
Z000001 01/01/1999 04/30/1999
Z000001 01/01/2000 12/31/2000
Z000001 01/01/2001 12/31/2001

I would like to be able to "roll these up" in order to reflect the
actual time frames so that it would like this:

ID StartDate EndDate
Z000001 01/01/1996 4/30/1999
Z000001 01/01/2000 12/31/2001

4/30/1999 to 01/01/2000 is, of course, a legitimate break where there
was a date difference of more than one day.

This appears to work, but you should test it with more dates. Note
that I am allowing the intervals to overlap.

It may not be that very efficient.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Thanks for the reply.

I'm using IE to view this so maybe it's my viewer's fault but I was not
able to see your script...only your disclaimer comments. :)

Nov 10 '06 #3

da******@aol.com wrote:
I'm trying to figure out how to this do with TSQL and preferably
through joins rather than by using cursors or temp tables. I have
multiple time spans for multiple individuals that I would like to
rollup together where they don't actually have a gap in coverage.

ID StartDate EndDate
Z000001 01/01/1996 9/30/1996
Z000001 10/01/1996 12/31/1998
Z000001 01/01/1999 04/30/1999
Z000001 01/01/2000 12/31/2000
Z000001 01/01/2001 12/31/2001

I would like to be able to "roll these up" in order to reflect the
actual time frames so that it would like this:

ID StartDate EndDate
Z000001 01/01/1996 4/30/1999
Z000001 01/01/2000 12/31/2001

4/30/1999 to 01/01/2000 is, of course, a legitimate break where there
was a date difference of more than one day.

Thanks in advance for any ideas.
Just the general idea, you can replace numbers with dates.

-- Assuming table numbers with 10K rows
CREATE TABLE Intervals(intFrom INT, intTo INT)
INSERT Intervals VALUES(1, 5)
INSERT Intervals VALUES(4, 7)
INSERT Intervals VALUES(8, 10)
INSERT Intervals VALUES(21, 35)
go
SELECT MIN(intFrom) intFrom, intTo FROM(
SELECT intFrom, MAX(intTo) intTo FROM(
SELECT i1.intFrom, i2.intTo FROM Intervals i1, Intervals i2
WHERE i1.intFrom <= i2.intFrom AND i1.intTo <= i2.intTo
AND (i2.intTo - i1.intFrom + 1) = (SELECT COUNT(*) FROM Numbers n
WHERE EXISTS(SELECT 1 FROM Intervals i WHERE n.Number BETWEEN
i.intFrom AND i.intTo)
AND n.Number BETWEEN i1.intFrom AND i2.intTo)
) t
GROUP BY intFrom
) t
GROUP BY intTo

intFrom intTo
----------- -----------
1 10
21 35

(2 row(s) affected)

-----------------------
Alex Kuznetsov
http://sqlserver-tips.blogspot.com/
http://sqlserver-puzzles.blogspot.com/

Nov 10 '06 #4

Erland Sommarskog wrote:
(da******@aol.com) writes:
I'm trying to figure out how to this do with TSQL and preferably
through joins rather than by using cursors or temp tables. I have
multiple time spans for multiple individuals that I would like to
rollup together where they don't actually have a gap in coverage.

ID StartDate EndDate
Z000001 01/01/1996 9/30/1996
Z000001 10/01/1996 12/31/1998
Z000001 01/01/1999 04/30/1999
Z000001 01/01/2000 12/31/2000
Z000001 01/01/2001 12/31/2001

I would like to be able to "roll these up" in order to reflect the
actual time frames so that it would like this:

ID StartDate EndDate
Z000001 01/01/1996 4/30/1999
Z000001 01/01/2000 12/31/2001

4/30/1999 to 01/01/2000 is, of course, a legitimate break where there
was a date difference of more than one day.

This appears to work, but you should test it with more dates. Note
that I am allowing the intervals to overlap.

It may not be that very efficient.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Erland, I don't see your query either

Nov 10 '06 #5
(da******@aol.com) writes:
I'm using IE to view this so maybe it's my viewer's fault but I was not
able to see your script...only your disclaimer comments. :)
Sorry, it appears that I forgot the essential part. Here it is:

CREATE TABLE periods (start datetime NOT NULL,
stop datetime NOT NULL,
CHECK (start < stop))
go
INSERT periods (start, stop)
VALUES ('19960101', '19990930')
INSERT periods (start, stop)
VALUES ('19960801', '19981231')
INSERT periods (start, stop)
VALUES ('19990101', '19990430')
INSERT periods (start, stop)
VALUES ('20000101', '20001231')
INSERT periods (start, stop)
VALUES ('20010101', '20011231')
go
SELECT a.start, MIN(b.stop)
FROM (SELECT start
FROM periods a
WHERE NOT EXISTS (SELECT *
FROM periods b
WHERE b.start < a.start
AND b.stop >= dateadd(DAY, -1, a.start))) AS a
CROSS JOIN
(SELECT stop
FROM periods a
WHERE NOT EXISTS (SELECT *
FROM periods b
WHERE b.stop a.stop
AND b.start <= dateadd(DAY, 1, a.stop))) AS b
WHERE a.start < b.stop
GROUP BY a.start
go
DROP TABLE periods

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Nov 11 '06 #6

Erland Sommarskog wrote:
(da******@aol.com) writes:
I'm using IE to view this so maybe it's my viewer's fault but I was not
able to see your script...only your disclaimer comments. :)

Sorry, it appears that I forgot the essential part. Here it is:

CREATE TABLE periods (start datetime NOT NULL,
stop datetime NOT NULL,
CHECK (start < stop))
go
INSERT periods (start, stop)
VALUES ('19960101', '19990930')
INSERT periods (start, stop)
VALUES ('19960801', '19981231')
INSERT periods (start, stop)
VALUES ('19990101', '19990430')
INSERT periods (start, stop)
VALUES ('20000101', '20001231')
INSERT periods (start, stop)
VALUES ('20010101', '20011231')
go
SELECT a.start, MIN(b.stop)
FROM (SELECT start
FROM periods a
WHERE NOT EXISTS (SELECT *
FROM periods b
WHERE b.start < a.start
AND b.stop >= dateadd(DAY, -1, a.start))) AS a
CROSS JOIN
(SELECT stop
FROM periods a
WHERE NOT EXISTS (SELECT *
FROM periods b
WHERE b.stop a.stop
AND b.start <= dateadd(DAY, 1, a.stop))) AS b
WHERE a.start < b.stop
GROUP BY a.start
go
DROP TABLE periods

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Well worth the wait. That was PERFECT. Many, many thanks for your
time. I had been relegated to using a WHILE statement to loop through
the records and test for a gap in time but wasn't able to return all
the spans...only one. Thanks again.

Have a great weekend.

Nov 11 '06 #7

Here's a view for invoicing that I had that finds the gaps (i.e.,
places where there is more than one day between the end of one invoice
and the start of the next (by activityid)

Backwards (from your perspective) but it may help you
/************************************************** ***************************************
***** Version 20060328_1155
**
** This view will list the invoice id and activity id for all invoices
that have no
** existing previous invoice, e.g., there is no invoice for the
activity that has a
** period ending corresponding to a listed invoices's period
beginning, e.g., the
** period beginning date for these invoices creates a gap in the
invoicing periods
** for the task (activity_id)
**
************************************************** ***************************************/
ALTER view dbo.viewInvoice_MissingPreviousInvoice
as
select t1.inv_id, t1.inv_activity_id
from tblinvoice_headers as t1
inner join (select inv_activity_id, min(inv_period_beginning) as
inv_period_beginning
from dbo.tblInvoice_Headers
group by inv_activity_id
) as t2 on t2.inv_activity_id = t1.inv_activity_id
where t1.inv_period_beginning <t2.inv_period_beginning
and not exists (select s1.inv_id
from dbo.tblInvoice_Headers as s1
where s1.inv_activity_id = t1.inv_activity_id
and datediff(day, s1.inv_period_ending,
t1.inv_period_beginning) = 1
)

Nov 11 '06 #8

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

Similar topics

11
by: Max M | last post by:
I am writing a "find-free-time" function for a calendar. There are a lot of time spans with start end times, some overlapping, some not. To find the free time spans, I first need to convert the...
6
by: TJ | last post by:
I've got a calendar that is based on the concept of lots of blocks that are spans with float:left. I would like to be able to have a detail section on the right side of the screen, so that when...
8
by: Joseph | last post by:
I am attempting to create a function that will hide all SPANS within an ided DIV ="idMain", then only display one span, but this function gives an error when it gets to elChildElement as being...
16
by: J. J. Cale | last post by:
Hi all I would like to see all the spans below inline one next to the other. I do not want to use absolute but relative or no positioning. This will eventually be a receipt that will be printed...
3
by: DB2 Convert | last post by:
Hi, Correct me if I am wrong? Why should I specify at the restore statement such as Restore Database ABC ... "WITHOUT ROLLING FOWARD"? My understand is when I am using circular logging,...
10
by: Evie | last post by:
I understand that when a switch statement is used without breaks, the code continues executing even after a matching case is found. Why, though, are subsequent cases not evaluated? I wrote a...
101
by: Elijah Cardon | last post by:
Let's say I have m dice having n sides, such that n^m is not going to bust int as a datatype. With m=4 and n=6, an outcome might be {2, 5, 1, 2}. What is a good way to represent this in c so that...
9
by: Steven Bethard | last post by:
I have some text and a list of Element objects and their offsets, e.g.:: ... (etree.Element('a'), 0, 21), ... (etree.Element('b'), 11, 18), ... (etree.Element('c'), 18, 18), ... ] ...
0
by: hummh | last post by:
Hi, I'm fighting with the asp.net 2.0 sitemappath control. I want to render breadcrumb items using <uland <litags, but the control mixes them with <spantags. That leads to invalid XHTML...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.