Overlapping Date SQL
Teradata Teradata Discussion Forums Teradata.com Discussion Forum
Visit Teradata.com
Home       Guidelines    Member List
Welcome Guest ( Login | Register )
        


This online forum is for user-to-user discussions of Teradata products, and is not an official customer support channel for Teradata. If you require direct assistance, please contact Teradata support.


Overlapping Date SQL Expand / Collapse
Author
Message
Posted 11/8/2007 1:09:22 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: Forum Members
Last Login: 3/27/2008 7:26:00 AM
Posts: 7, Visits: 9
I need some help with SQL to deal with overlapping dates. Here's the table I have

Customer ID Communication ID Start Date End Date
100 9 1/1/2006 3/30/2006
100 10 2/10/2006 4/10/2006
100 11 5/10/2006 6/30/2006
200 9 1/1/2006 3/30/2006
200 12 1/15/2006 3/15/2006
300 9 1/1/2006 3/30/2006
400 9 1/1/2006 3/30/2006
400 10 2/10/2006 4/10/2006
400 12 1/15/2006 3/15/2006

I need counts of customers with counts of the communication IDs where the start date and end dates overlap.

Results from above would be

count of customers count of communication IDs
2 2
1 3


Thanks!
Post #9664
Posted 11/9/2007 4:42:54 AM
Supreme Being

Supreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme Being

Group: Forum Members
Last Login: Yesterday @ 9:37:55 AM
Posts: 487, Visits: 217
If the number of rows per CustomerID is low there's a simple solution:

select cnt, count(*)
from
(
select t1.CustomerID, count(*) as cnt
from tab t1 join tab t2
on t1.CustomerID = t2.CustomerID
and t1.CommunicationID < t2.CommunicationID
where (t1.StartDate, t1.EndDate) overlaps (t2.StartDate, t2.EndDate)
group by 1
) dt
group by 1

Another one had to use nested OLAP functions...

Dieter
Post #9667
Posted 11/9/2007 4:46:25 AM
Supreme Being

Supreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme Being

Group: Forum Members
Last Login: Yesterday @ 9:37:55 AM
Posts: 487, Visits: 217
Ooops, i can't delete that previous post.
I just recognized that it will not work, i'll have to think about another solution.

Dieter
Post #9669
Posted 11/9/2007 5:39:43 AM
Supreme Being

Supreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme Being

Group: Forum Members
Last Login: Yesterday @ 9:37:55 AM
Posts: 487, Visits: 217
This time i actually tested the query :-)

select
comm_cnt,
count(*)
from
(
select CustomerID,
max(overlap) as comm_cnt
from
(
select CustomerID, Dat,
sum(event) over (partition by CustomerID
order by dat, event
rows unbounded preceding ) as overlap
from
(
Select
CustomerID, StartDate as dat, 1 as event
From U999999.dropme
union all
Select
CustomerID, EndDate as dat, -1 as event
From U999999.dropme
) dt
qualify overlap > 1
)dt
group by 1
) dt
group by 1


But you didn't tell what's the expected result set if there are several seperate overlapping date ranges for the same customer, e.g. first with 3 overlapping and second with 2 overlapping.

My query just uses the maximum number. if you want to count both there must be 2? OLAP steps...

Dieter
Post #9670
« Prev Topic | Next Topic »


Reading This Topic Expand / Collapse
Active Users: 0 ( 0 guests, 0 members, 0 anonymous members )
No members currently viewing this topic.


All times are GMT -5:00, Time now is 8:04am

Powered By InstantForum.NET v4.1.4 © 2008
Execution: 0.063. 7 queries. Compression Disabled.