Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Start and End Dates for Historical Data - SQL 2008R2 version of Lead/Lag? Expand / Collapse
Author
Message
Posted Wednesday, October 16, 2013 3:04 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Wednesday, July 16, 2014 5:32 PM
Points: 388, Visits: 1,593
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



Post #1505421
Posted Thursday, October 17, 2013 12:54 PM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: 2 days ago @ 5:20 AM
Points: 950, Visits: 2,876
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);

Post #1505875
Posted Thursday, October 17, 2013 6:56 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: 2 days ago @ 6:01 PM
Points: 3,609, Visits: 5,222
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!
Post #1505956
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse