September 11, 2013 at 5:15 am
I want to reference a previous group's max date and compare it to the date on the current record.
My data looks like this:-
Grp - Date
00 - 28-10-2012
10 - 29-10-2012
10 - 28-10-2012
10 - 28-10-2012
20 - 30-10-2012
20 - 05-11-2012
20 - 10-11-2012
20 - 30-10-2012
30 - 01-11-2012
So we can that for Grp 10, the max date is 29-10-2012, for Grp 20, the max date is 10-11-2012, etc...
I want to reference the previous group's max date and compare it to the date on the current record.
For example, for Grp 20, dated 05-11-2012, it needs to be compared to the max date for Grp 10 (29-10-2012) to calculate the difference in dates.
Grp - Date - Previous Date - Delay
00 - 28-10-2012 -
10 - 29-10-2012 - 28-10-2012 - 1
10 - 28-10-2012 - 28-10-2012 - 0
10 - 28-10-2012 - 28-10-2012 - 0
20 - 30-10-2012 - 29-10-2012 - 1
20 - 05-11-2012 - 29-10-2012 - 7
20 - 10-11-2012 - 29-10-2012 - 12
20 - 30-10-2012 - 29-10-2012 - 1
30 - 12-11-2012 - 10-11-2012 - 2
Is this possible in TSQL or do i need to write Package/Function?
Any guidance would be appreciated.
September 11, 2013 at 6:13 am
Your requirement is not clear to me...
On what basis you are doing rhis thing-
for Grp 20, dated 05-11-2012, it needs to be compared to the max date for Grp 10 (29-10-2012) to calculate the difference in dates.
_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
September 11, 2013 at 6:28 am
SET DATEFORMAT DMY;
DECLARE @t TABLE(Grp CHAR(2), dt Date);
INSERT INTO @t(Grp,dt)
VALUES
('00' , '28-10-2012'),
('10' , '29-10-2012'),
('10' , '28-10-2012'),
('10' , '28-10-2012'),
('20' , '30-10-2012'),
('20' , '05-11-2012'),
('20' , '10-11-2012'),
('20' , '30-10-2012'),
('30' , '12-11-2012');
SELECT t.Grp,
t.dt AS [Date],
ca.PreviousDate,
DATEDIFF(Day,ca.PreviousDate,t.dt) AS Delay
FROM @t t
OUTER APPLY (SELECT TOP 1 t2.dt
FROM @t t2
WHERE t2.Grp < t.Grp
ORDER BY t2.Grp DESC,t2.dt DESC) ca(PreviousDate)
ORDER BY 1,2;
____________________________________________________
Deja View - The strange feeling that somewhere, sometime you've optimised this query before
How to get the best help on a forum
http://www.sqlservercentral.com/articles/Best+Practices/61537September 11, 2013 at 7:00 am
good one Mark 🙂
_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
September 11, 2013 at 7:34 am
Yay - thats great Mark! many Thanks, and apologies for not following the right way to set up the questionin the first place - I will definately try and follow your guidance in the future.
September 11, 2013 at 7:35 am
tim.oliver (9/11/2013)
Yay - thats great Mark! many Thanks, and apologies for not following the right way to set up the questionin the first place - I will definately try and follow your guidance in the future.
You're welcome!
____________________________________________________
Deja View - The strange feeling that somewhere, sometime you've optimised this query before
How to get the best help on a forum
http://www.sqlservercentral.com/articles/Best+Practices/61537Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply