SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Start and End Dates for Historical Data - SQL 2008R2 version of Lead/Lag?


Start and End Dates for Historical Data - SQL 2008R2 version of Lead/Lag?

Author
Message
MWise
MWise
SSChasing Mays
SSChasing Mays (620 reputation)SSChasing Mays (620 reputation)SSChasing Mays (620 reputation)SSChasing Mays (620 reputation)SSChasing Mays (620 reputation)SSChasing Mays (620 reputation)SSChasing Mays (620 reputation)SSChasing Mays (620 reputation)

Group: General Forum Members
Points: 620 Visits: 1797
I'm guessing that this is some sort of mashup of islands and running total code. I have historical data about phone numbers and I need to summarize the daily data into interval records to load into a historical dimension. On every change of phone location or type, I need to add a new record with the effective start and end dates. Here's some sample data

Create table #A
(PhoneNum varchar(10),
PhoneType char(1),
PhoneLocation char(1),
FileDate date)

INSERT INTO #A
VALUES ('0000000000','A','B','01/01/2013'),
('0000000000','A','B','01/05/2013'),
('0000000000','A','B','01/10/2013'),

('0000000000','G','B','01/12/2013'),
('0000000000','G','B','01/13/2013'),
('0000000000','G','B','01/14/2013'),

('0000000000','A','B','01/18/2013'),
('0000000000','A','B','01/19/2013'),


('0000000001','A','V','01/01/2013'),
('0000000001','A','V','01/07/2013'),

('0000000001','X','Y','01/17/2013'),

('0000000001','G','B','01/18/2013'),
('0000000001','G','B','01/19/2013'),

('0000000001','A','B','01/21/2013')


SELECT PhoneNum,
PhoneType,
PhoneLocation,
MIN(FileDate) as StartDate,
MAX(FileDate) as EndDate
FROM #A
GROUP BY PhoneNum,
PhoneType,
PhoneLocation
ORDER BY PhoneNum,
MIN(FileDate)



The results from the select statement work fine for phone number '0000000001', but not for '0000000000'. The results I want are:

PhoneNum PhoneType PhoneLocation StartDate EndDate
0000000000 A B 2013-01-01 2013-01-10
0000000000 G B 2013-01-12 2013-01-14
0000000000 A B 2013-01-18 2013-01-19
0000000001 A V 2013-01-01 2013-01-07
0000000001 X Y 2013-01-17 2013-01-17
0000000001 G B 2013-01-18 2013-01-19
0000000001 A B 2013-01-21 2013-01-21



I need to somehow set a grouping id if the previous values of PhoneType and PhoneLocation of a PhoneNumber change based on the order of the FileDate. If I had 2012, I could use LEAD/LAG right? Once I have the grouping, I should be able to pull the MIN/MAX FileDate to get my Start and End Dates. Any help would be much appreciated.

Thanks,
MWise



mickyT
mickyT
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1610 Visits: 3317
Hi

This should do the trick for you
with cte as (
select PhoneNum, PhoneType, PhoneLocation, FileDate
,ROW_NUMBER() OVER (PARTITION BY PhoneNum ORDER BY PhoneType, PhoneLocation, FileDate) -
ROW_NUMBER() OVER (PARTITION BY PhoneNum ORDER BY FileDate) R
FROM #A
)
SELECT PhoneNum, PhoneType, PhoneLocation, Min(FileDate), max(FileDate)
FROM cte
GROUP BY PhoneNum, PhoneType, PhoneLocation, R
ORDER BY PhoneNum, Min(fileDate);


dwain.c
dwain.c
SSCertifiable
SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)

Group: General Forum Members
Points: 7571 Visits: 6431
MWise (10/16/2013)
I need to somehow set a grouping id if the previous values of PhoneType and PhoneLocation of a PhoneNumber change based on the order of the FileDate. If I had 2012, I could use LEAD/LAG right? Once I have the grouping, I should be able to pull the MIN/MAX FileDate to get my Start and End Dates. Any help would be much appreciated.

Thanks,
MWise


MickyT's solution (which earns him a +1 BTW) is probably the best you're going to do for solving this (an islands) problem.

I've found the SQL 2012 LAG/LEAD functions very good at coming up with Gaps, which can be converted into Islands (The SQL of Gaps and Islands in Sequences), using CROSS APPLY VALUES. I'm just not sure how that would compare to the straight up islands approach MickyT did with respect to performance.

Perhaps there is a direct solution to using LAG/LEAD to give you Islands, I just haven't hit upon it yet (nor have I tried to search for it).


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search