Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 How to get start and end date without breaches?

Author  Topic 

djpirra
Starting Member

6 Posts

Posted - 2010-03-15 : 18:48:44
Hi there,

I have a services table that have customer signed services... all services have a start and an end date. Is it possible for sql server to determine the last start and end dates by customer without breaches?

Example table
Customer Service Start Date End Date
1 1 2000-01-01 2001-01-01
1 2 2001-01-02 2009-12-10
1 3 2010-01-01 2006-02-01
2 1 2000-01-01 2010-01-01
2 2 2005-10-01 2009-12-31
2 3 2009-01-01 2009-04-31

In the customer 1 the result should be:
2010-01-01 - 2010-02-01 (Because between 2009-12-10 and 2010-01-01 theres no service)
In the customer 2 the result should be:
2000-01-01 - 2009-12-31

Hope i was clear enought.

Thank you.

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2010-03-15 : 18:59:51
what do you mean without breaches ?

Is it possible that the End Date is less than the Start Date ?
"1 3 2010-01-01 2006-02-01"

Can you explain again how do you get the required Start and End date for each customer ?


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

djpirra
Starting Member

6 Posts

Posted - 2010-03-15 : 19:09:09
Well when i mean without breaches, i want to say that i customer can have multiple services at same time... i want to get the first start date and last end date of the services that overlaps in terms of timeline.

imagine this lines as examples (1,2 and 3 are services for same customer):

1.|--------------|
2..............|-----------------|
3..........|---------------------------------|

In this case i want start date from 1 and end date from 3... this is easy...next...

1.|------------|
2.....|------------|
3.........................|-------------------|

In this i want all dates from 3....

1.|-------|
2...............|-----------|
3.........................|-------------|

I want start date from 2 and end date from 3... Got it? Its not easy for me to explain :)
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2010-03-15 : 19:22:24
Sorry, I am still confuse

quote:
1.|--------------|
2............|-----------------|
3......|-----------------|

In this case i want start date from 1 and end date from 3... this is easy

why end date from 3 ? Isn't it end date of 2 is later than 3 ?

quote:
1.|------------|
2....|------------|
3....................|-------------------|

In this i want all dates from 3....

Why not start date from 1 ?




KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

djpirra
Starting Member

6 Posts

Posted - 2010-03-15 : 19:30:31
Sorry... the spaces went bad...
Now its correct... please check :)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-16 : 02:44:11
seems like this is what you're looking for:-


CREATE TABLE #Table
(
Customer int,
[Service] int,
StartDate datetime,
EndDate datetime
)
INSERT #Table
SELECT 1, 1, '20000101' ,'20010101' UNION ALL
SELECT 1, 2, '20010102', '20091210' UNION ALL
SELECT 1, 3, '20100101', '20100201' UNION ALL
SELECT 2, 1, '20000101', '20030101' UNION ALL
SELECT 2, 2, '20051001', '20091231' UNION ALL
SELECT 2, 3, '20090101', '20090430' UNION ALL
SELECT 3, 1, '20050101', '20060101' UNION ALL
SELECT 3, 2, '20061001', '20091231' UNION ALL
SELECT 3, 3, '20090101', '20100310' UNION ALL
SELECT 4, 1, '20050101', '20061201' UNION ALL
SELECT 4, 2, '20061001', '20071231' UNION ALL
SELECT 4, 3, '20080101', '20090210' UNION ALL
SELECT 4, 4, '20080501', '20090101' UNION ALL
SELECT 4, 5, '20081001', '20091231' UNION ALL
SELECT 4, 6, '20090101', '20090310' UNION ALL
SELECT 4, 7, '20100101', '20100125' UNION ALL
SELECT 4, 8, '20100120', '20100130' UNION ALL
SELECT 4, 9, '20100112', '20100310'



;With CTE
AS
(
SELECT t.Customer,
CASE WHEN t1.MinDate IS NOT NULL AND t1.MinDate<t.Startdate THEN t1.MinDate ELSE t.StartDate END AS StartDate,
CASE WHEN t1.MaxDate IS NOT NULL AND t1.MaxDate>t.Enddate THEN t1.MaxDate ELSE t.EndDate END AS EndDate,
DENSE_RANK() OVER (PARTITION BY Customer ORDER BY CASE WHEN t1.MinDate IS NOT NULL AND t1.MinDate<t.Startdate THEN t1.MinDate ELSE t.StartDate END,
CASE WHEN t1.MaxDate IS NOT NULL AND t1.MaxDate>t.Enddate THEN t1.MaxDate ELSE t.EndDate END)
AS Overlapped
FROM #TAble t
OUTER APPLY (SELECT MIN(StartDate) AS MinDate,MAX(EndDate) AS MaxDate,COUNT(*) AS cnt
FROM #TAble
WHERE Customer=t.Customer
AND [Service] <> t.[Service]
AND (t.EndDate BETWEEN StartDate AND EndDate
OR t.StartDate BETWEEN StartDate AND EndDate)
)t1

)

SELECT c1.Customer,StartDate,EndDate
FROM CTE c1
INNER JOIN (SELECT Customer,MAX(Overlapped) AS MaxOverlap
FROM CTE
GROUP BY Customer)c2
ON c2.Customer=c1.Customer
AND c2.MaxOverlap=c1.Overlapped
GROUP BY c1.Customer,StartDate,EndDate

DROP TABLE #Table

output
--------------------------------------
entered data
Customer Service StartDate EndDate
1 1 2000-01-01 00:00:00.000 2001-01-01 00:00:00.000
1 2 2001-01-02 00:00:00.000 2009-12-10 00:00:00.000
1 3 2010-01-01 00:00:00.000 2010-02-01 00:00:00.000
2 1 2000-01-01 00:00:00.000 2003-01-01 00:00:00.000
2 2 2005-10-01 00:00:00.000 2009-12-31 00:00:00.000
2 3 2009-01-01 00:00:00.000 2009-04-30 00:00:00.000
3 1 2005-01-01 00:00:00.000 2006-01-01 00:00:00.000
3 2 2006-10-01 00:00:00.000 2009-12-31 00:00:00.000
3 3 2009-01-01 00:00:00.000 2010-03-10 00:00:00.000
4 1 2005-01-01 00:00:00.000 2006-12-01 00:00:00.000
4 2 2006-10-01 00:00:00.000 2007-12-31 00:00:00.000
4 3 2008-01-01 00:00:00.000 2009-02-10 00:00:00.000
4 4 2008-05-01 00:00:00.000 2009-01-01 00:00:00.000
4 5 2008-10-01 00:00:00.000 2009-12-31 00:00:00.000
4 6 2009-01-01 00:00:00.000 2009-03-10 00:00:00.000
4 7 2010-01-01 00:00:00.000 2010-01-25 00:00:00.000
4 8 2010-01-20 00:00:00.000 2010-01-30 00:00:00.000
4 9 2010-01-12 00:00:00.000 2010-03-10 00:00:00.000


output data
Customer StartDate EndDate
1 2010-01-01 00:00:00.000 2010-02-01 00:00:00.000
2 2005-10-01 00:00:00.000 2009-12-31 00:00:00.000
3 2006-10-01 00:00:00.000 2010-03-10 00:00:00.000
4 2010-01-01 00:00:00.000 2010-03-10 00:00:00.000



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

djpirra
Starting Member

6 Posts

Posted - 2010-03-16 : 11:51:21
Hey there,

I have tried it but somehow its not working fast...
My table have like 15M rows... and i need to flatten the time for all customers. 3H running and nothing returned...
If i run for a single customer it seems fast.

What todo?

Regards
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-16 : 11:55:51
quote:
Originally posted by djpirra

Hey there,

I have tried it but somehow its not working fast...
My table have like 15M rows... and i need to flatten the time for all customers. 3H running and nothing returned...
If i run for a single customer it seems fast.

What todo?

Regards


analyse the excution plan and see if you can add any indexes to speed up query


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

djpirra
Starting Member

6 Posts

Posted - 2010-03-16 : 12:08:08
I already did... Execution Plan shows all indexes beeing used...
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-16 : 12:12:08
you always run query for all 15 m records?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

djpirra
Starting Member

6 Posts

Posted - 2010-03-16 : 12:36:54
Yes always.... Because i need a snapshot behavior...
Go to Top of Page
   

- Advertisement -