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

Sql Query Expand / Collapse
Author
Message
Posted Monday, September 9, 2013 5:16 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, September 26, 2014 2:36 PM
Points: 189, Visits: 688
I have the following data.

RowId DateStamp prevStatus CurrentStatus
1 6/1/2012 null 8
2 6/2/2012 8 9
3 6/8/2012 9 8
4 6/10/2012 8 9
5 6/12/2012 9 8
6 6/16/2012 8 9
7 6/19/2012 9 8

Based on the above data I need to calculate the date difference between the status change between 8 to 8,the time diff between two current status 8. Time diff between rowid 1 and 3,3 and 5 ,5 and 7...

Can anyone help me with the query?

Thanks.
Post #1492731
Posted Monday, September 9, 2013 7:04 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 12:13 PM
Points: 10,342, Visits: 13,351
Something like this should work:

DECLARE @test TABLE
(
RowID INT,
DateStamp DATE,
PrevStatus INT,
CurrentStatus INT
);

INSERT INTO @test
(RowID, DateStamp, PrevStatus, CurrentStatus)
VALUES
(1, '6/1/2012', NULL, 8),
(2, '6/2/2012', 8, 9),
(3, '6/8/2012', 9, 8),
(4, '6/10/2012', 8, 9),
(5, '6/12/2012', 9, 8),
(6, '6/16/2012', 8, 9),
(7, '6/19/2012', 9, 8);

WITH data
AS (
SELECT
*,
ROW_NUMBER() OVER (PARTITION BY T.CurrentStatus ORDER BY T.DateStamp) AS rowNo
FROM
@test AS T
)
SELECT
*,
DATEDIFF(DAY, D1.DateStamp, D2.DateStamp) AS StatusDateDiff
FROM
data AS D1
LEFT JOIN data AS D2
ON D1.rowNo = D2.rowNo - 1 AND
D1.CurrentStatus = D2.CurrentStatus

Please note how I provided the data in way that allows for testing.




Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Post #1492761
Posted Monday, September 9, 2013 8:03 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Yesterday @ 10:20 AM
Points: 4,437, Visits: 6,339
Since you are on SQL 2012, new Windowing Functionality is available too. Here is a solution based on that. Note you may want to add in columns and/or handle missing endpoint(s).

;WITH a AS (SELECT datestamp AS datestart, currentstatus, lead(datestamp,2) OVER(ORDER BY datestamp) AS dateend
FROM @test)
SELECT DATEDIFF(dd, datestart, dateend) AS dateinterval
FROM a
WHERE currentstatus = 8
AND dateend IS NOT NULL



Best,

Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru at GMail
Post #1492795
Posted Tuesday, September 10, 2013 4:40 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, April 10, 2014 3:46 AM
Points: 6, Visits: 178
DECLARE @test TABLE
(
RowID INT,
DateStamp DATE,
PrevStatus INT,
CurrentStatus INT
);

INSERT INTO @test
(RowID, DateStamp, PrevStatus, CurrentStatus)
VALUES
(1, '6/1/2012', NULL, 8),
(2, '6/2/2012', 8, 9),
(3, '6/8/2012', 9, 8),
(4, '6/10/2012', 8, 9),
(5, '6/12/2012', 9, 8),
(6, '6/16/2012', 8, 9),
(7, '6/19/2012', 9, 8);



SELECT * INTO #TEST1 FROM
(
SELECT *
FROM @test
WHERE currentstatus = 8
AND PrevStatus IS NOT NULL
)A

select * from #TEST1
;WITH CTE AS
(
SELECT *, ROW_NUMBER() OVER (ORDER BY DateStamp) AS RowNumber
FROM #TEST1
)
SELECT A.RowID,B.rowid,DATEDIFF(dd, A.DateStamp, B.DateStamp) as 'DATEPART'
FROM CTE A
JOIN CTE B
ON A.RowNumber = B.RowNumber - 1

DROP TABLE #TEST1

Post #1493092
Posted Tuesday, September 10, 2013 7:22 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 @ 4:53 AM
Points: 3,422, Visits: 5,368
My personal preference is to do this with a CROSS APPLY:

WITH SampleData AS (
SELECT RowID, DateStamp=CAST(DateStamp AS DATE), PrevStatus, CurrentStatus
FROM (VALUES
(1, '6/1/2012', NULL, 8),
(2, '6/2/2012', 8, 9),
(3, '6/8/2012', 9, 8),
(4, '6/10/2012', 8, 9),
(5, '6/12/2012', 9, 8),
(6, '6/16/2012', 8, 9),
(7, '6/19/2012', 9, 8)) a(RowID, DateStamp, PrevStatus, CurrentStatus))
SELECT a.RowID, a.DateStamp, PrevStatus, CurrentStatus
,ElapsedDays=DATEDIFF(day, b.DateStamp, a.DateStamp)
FROM SampleData a
CROSS APPLY (
SELECT TOP 1 DateStamp
FROM SampleData b
WHERE a.CurrentStatus = b.CurrentStatus AND b.DateStamp < a.DateStamp
ORDER BY b.DateStamp DESC) b
WHERE a.CurrentStatus = 8
ORDER BY a.RowID;


At least it would have been in SQL versions earlier than 2012. TheSQLGuru's solution for SQL 2012 is probably faster. Haven't had the chance to test it yet myself as I just got 2012 this week. Soon though.



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 #1493416
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse