SQLServerCentral.com / SQL Server 2012 / SQL Server 2012 - T-SQL / DateDiff one Date column of First Row and the other Date column of second row. / Latest PostsInstantForum.NET v2.9.0SQLServerCentral.comhttp://www.sqlservercentral.com/Forums/notifications@sqlservercentral.comSun, 02 Aug 2015 17:20:16 GMT20RE: DateDiff one Date column of First Row and the other Date column of second row.http://www.sqlservercentral.com/Forums/Topic1536160-3077-1.aspxYep, as Sean said I assumed that you use SQL 2012 and windows offset functions are a good choice in situations where you want to refer to some other row from your row.Anyway, if you cannot use SQL 2012 the following query is logically equivalent to the query I provided in previous post:[code="sql"]SELECT RowID, CASE WHEN DATEDIFF(DAY, Date2, ( SELECT TOP 1 Date1 FROM YourTable t2 WHERE t1.IDNo = t2.IDNo AND t1.RowId &lt; t2.RowId ORDER BY t2.RowId ASC ) ) &lt; 14 THEN 1 ELSE 0 END IsDateDiffLess14_Calculation FROM YourTable t1ORDER BY RowID[/code]Thu, 30 Jan 2014 14:29:07 GMTmilos.radivojevicRE: DateDiff one Date column of First Row and the other Date column of second row.http://www.sqlservercentral.com/Forums/Topic1536160-3077-1.aspx[quote][b]KtmGuy (1/30/2014)[/b][hr]What is LEAD(Date1) ??? 'LEAD' is not a recognized built-in function name.[/quote]That is one of the analytical functions introduced in 2012. Since you posted in a 2012 forum it was assumed your were using 2012. What version of sql server are you using?Thu, 30 Jan 2014 12:15:30 GMTSean LangeRE: DateDiff one Date column of First Row and the other Date column of second row.http://www.sqlservercentral.com/Forums/Topic1536160-3077-1.aspxWhat is LEAD(Date1) ??? 'LEAD' is not a recognized built-in function name.Thu, 30 Jan 2014 11:04:11 GMTKtmGuyRE: DateDiff one Date column of First Row and the other Date column of second row.http://www.sqlservercentral.com/Forums/Topic1536160-3077-1.aspxHere is a piece of code which calculates if the flag IsDateDiffLess14_Calculation should be set or not.[code="sql"]SELECT RowID, CASE WHEN DATEDIFF(DAY, Date2, LEAD(Date1) OVER(PARTITION BY IDNo ORDER BY RowId)) &lt; 14 THEN 1 ELSE 0 END IsDateDiffLess14_Calculation FROM YourTableORDER BY RowID[/code]I am not sure if it's required to have this flag as persistent column in the table. If it is you should modify the script and use it whenever a new row is added to the table to update the previous row only and not the whole table.Thu, 30 Jan 2014 02:13:26 GMTmilos.radivojevicDateDiff one Date column of First Row and the other Date column of second row.http://www.sqlservercentral.com/Forums/Topic1536160-3077-1.aspxIs there a efficient way to compare two different columns of 2 different rows in a data set as shown below. For eg: I would like to DateDiff between Date2 of RowID 1 and Date1 of RowID 2 of IDNo 123. After this comparision , if datediff between two dates are &lt;=14 then i want to update 1 else 0 in IsDateDiffLess14 of RowID1 . In below example its 0 because datediff of two dates &gt;=14. So, want to compare the Date2 and Date1 in this sequence for the same IDNo. For RowID 6 there is only 1 row and no other row to compare, in this case IsDateDiffLess14 should be updated with 0. RowID IDNo Date1 Date2 IsDateDiffLess141 123 04/10/2013 04/12/2013 02 123 05/10/2013 05/11/2013 13 123 05/21/2013 05/25/2013 0 4 112 01/10/2013 01/14/2013 15 112 01/27/2013 01/28/2013 06 120 03/10/2013 03/12/2013 0I really appreiciate for any kind of feedback in this regard. Thanks, Wed, 29 Jan 2014 21:53:13 GMTKtmGuy