SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Date Diff
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

shilpash
Yak Posting Veteran

72 Posts

Posted - 05/04/2012 :  13:18:21  Show Profile  Reply with Quote
ln ed
29845 2011-01-07
29845 2011-01-19
29845 2011-01-28
29845 2011-02-01
29845 2011-02-09
29845 2011-02-28


I need to find the diff between the ed to the next ed in new column.so in first case 0,then diff b/w 2011-01-07 and 2011-01-19,then diff b/w 2011-01-19 and 2011-01-28 and so on.
Thanks

Lamprey
Flowing Fount of Yak Knowledge

3833 Posts

Posted - 05/04/2012 :  13:45:18  Show Profile  Reply with Quote
Check the DATEDIFF function:
http://msdn.microsoft.com/en-us/library/ms189794.aspx
Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5152 Posts

Posted - 05/04/2012 :  13:46:25  Show Profile  Reply with Quote
This:
select
	a.*,
	datediff(dd,coalesce(b.ed,a.ed),a.ed)
from
	YourTable a
	outer apply
	( select top 1 x.ed from YourTable x where x.ed <a.ed order by x.ed desc) b
On the outside chance that you are on SQL 2012, it is a little easier and perhaps faster to do it like this:
select
	*,
	datediff(dd,COALESCE(max(ed) over( order by ed rows between 1 preceding and 1 preceding ),ed), ed)
from
	YourTable
Go to Top of Page

shilpash
Yak Posting Veteran

72 Posts

Posted - 05/04/2012 :  13:46:43  Show Profile  Reply with Quote
WITH cte
AS (SELECT ln
,ed
FROM #abc
)
SELECT [current].ln
,[current].ed
,DATEDIFF(DAY,next.ed,[current].ed)
FROM cte AS [current]
LEFT JOIN cte AS [next]
ON [next].ed = (SELECT MIN(ed)
FROM cte


I have come this so far,,but actually i need the diff between two consecutives,,above query gives me this result---

ln ed (No column name)
29845 2011-01-07 0
29845 2011-01-19 12
29845 2011-01-28 21
29845 2011-02-01 25
29845 2011-02-09 33
29845 2011-02-28 52
Go to Top of Page

shilpash
Yak Posting Veteran

72 Posts

Posted - 05/04/2012 :  13:48:37  Show Profile  Reply with Quote
quote:
Originally posted by sunitabeck

This:
select
	a.*,
	datediff(dd,coalesce(b.ed,a.ed),a.ed)
from
	YourTable a
	outer apply
	( select top 1 x.ed from YourTable x where x.ed <a.ed order by x.ed desc) b
On the outside chance that you are on SQL 2012, it is a little easier and perhaps faster to do it like this:
select
	*,
	datediff(dd,COALESCE(max(ed) over( order by ed rows between 1 preceding and 1 preceding ),ed), ed)
from
	YourTable





Great.Thanks Sunita.Superb

Edited by - shilpash on 05/04/2012 13:49:24
Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5152 Posts

Posted - 05/04/2012 :  15:27:24  Show Profile  Reply with Quote
You are welcome. Glad to help.)
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
29138 Posts

Posted - 05/04/2012 :  16:51:53  Show Profile  Visit SwePeso's Homepage  Reply with Quote
Why use MAX instead if LEAD or LAG?
DECLARE	@Sample TABLE
	(
		[In] INT,
		Ed DATE
	)

INSERT	@Sample
VALUES	(29845, '2011-01-07'),
	(29845, '2011-01-19'),
	(29845, '2011-01-28'),
	(29845, '2011-02-01'),
	(29845, '2011-02-09'),
	(29845, '2011-02-28')

-- SwePeso
SELECT	[In],
	Ed,
	DATEDIFF(DAY, Ed, LEAD(Ed) OVER (PARTITION BY [In] ORDER BY Ed)),
	DATEDIFF(DAY, Ed, LEAD(Ed, 1, Ed) OVER (PARTITION BY [In] ORDER BY Ed)),
	DATEDIFF(DAY, Ed, LAG(Ed) OVER (PARTITION BY [In] ORDER BY Ed)),
	DATEDIFF(DAY, Ed, LAG(Ed, 1, Ed) OVER (PARTITION BY [In] ORDER BY Ed))
FROM	@Sample


N 56°04'39.26"
E 12°55'05.63"

Edited by - SwePeso on 05/04/2012 16:53:22
Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5152 Posts

Posted - 05/04/2012 :  17:18:02  Show Profile  Reply with Quote
quote:
Originally posted by SwePeso

Why use MAX instead if LEAD or LAG?

Ha! That is easy to answer. Because I haven't gotten that far in BOL.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000