<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Self Join &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/self-join/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.dbrnd.com</link>
	<description>BIGData &#124; NoSQL &#124; MSSQL &#124; MySQL &#124; PostgreSQL</description>
	<lastBuildDate>Fri, 29 Sep 2017 11:43:11 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.6</generator>
	<item>
		<title>SQL Server 2012: Introduced LAG function, the best alternative of Self Join to compare Previous Row</title>
		<link>https://www.dbrnd.com/2016/02/sql-server-2012-introduced-lag-function-the-best-alternative-of-self-join-to-compare-previous-row/</link>
					<comments>https://www.dbrnd.com/2016/02/sql-server-2012-introduced-lag-function-the-best-alternative-of-self-join-to-compare-previous-row/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Mon, 08 Feb 2016 18:43:18 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[LAG]]></category>
		<category><![CDATA[Self Join]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server Administrator]]></category>
		<category><![CDATA[SQL Server Monitoring]]></category>
		<category><![CDATA[SQL Server Performance Tunning]]></category>
		<category><![CDATA[SQL Server Tips and Tricks]]></category>
		<category><![CDATA[TSQL]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=1508</guid>

					<description><![CDATA[In this post, I am going to demonstrate one of new LAG() function which is introduced by SQL Server 2012. Generally, we are using Self Join to perform row-level analysis by comparing Next and Previous rows. SQL Server 2012 has a LAG() function is used for accessing data from the Previous Row in the same [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F02%2Fsql-server-2012-introduced-lag-function-the-best-alternative-of-self-join-to-compare-previous-row%2F&amp;linkname=SQL%20Server%202012%3A%20Introduced%20LAG%20function%2C%20the%20best%20alternative%20of%20Self%20Join%20to%20compare%20Previous%20Row" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F02%2Fsql-server-2012-introduced-lag-function-the-best-alternative-of-self-join-to-compare-previous-row%2F&amp;linkname=SQL%20Server%202012%3A%20Introduced%20LAG%20function%2C%20the%20best%20alternative%20of%20Self%20Join%20to%20compare%20Previous%20Row" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F02%2Fsql-server-2012-introduced-lag-function-the-best-alternative-of-self-join-to-compare-previous-row%2F&amp;linkname=SQL%20Server%202012%3A%20Introduced%20LAG%20function%2C%20the%20best%20alternative%20of%20Self%20Join%20to%20compare%20Previous%20Row" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F02%2Fsql-server-2012-introduced-lag-function-the-best-alternative-of-self-join-to-compare-previous-row%2F&amp;linkname=SQL%20Server%202012%3A%20Introduced%20LAG%20function%2C%20the%20best%20alternative%20of%20Self%20Join%20to%20compare%20Previous%20Row" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F02%2Fsql-server-2012-introduced-lag-function-the-best-alternative-of-self-join-to-compare-previous-row%2F&amp;linkname=SQL%20Server%202012%3A%20Introduced%20LAG%20function%2C%20the%20best%20alternative%20of%20Self%20Join%20to%20compare%20Previous%20Row" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F02%2Fsql-server-2012-introduced-lag-function-the-best-alternative-of-self-join-to-compare-previous-row%2F&amp;linkname=SQL%20Server%202012%3A%20Introduced%20LAG%20function%2C%20the%20best%20alternative%20of%20Self%20Join%20to%20compare%20Previous%20Row" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F02%2Fsql-server-2012-introduced-lag-function-the-best-alternative-of-self-join-to-compare-previous-row%2F&#038;title=SQL%20Server%202012%3A%20Introduced%20LAG%20function%2C%20the%20best%20alternative%20of%20Self%20Join%20to%20compare%20Previous%20Row" data-a2a-url="https://www.dbrnd.com/2016/02/sql-server-2012-introduced-lag-function-the-best-alternative-of-self-join-to-compare-previous-row/" data-a2a-title="SQL Server 2012: Introduced LAG function, the best alternative of Self Join to compare Previous Row"></a></p><p>In this post, I am going to demonstrate one of new LAG() function which is introduced by SQL Server 2012.</p>
<blockquote><p>Generally, we are using Self Join to perform row-level analysis by comparing Next and Previous rows.<br />
SQL Server 2012 has a LAG() function is used for accessing data from the Previous Row in the same result set.</p></blockquote>
<p>For example, we have a different transaction date in our table and require to find the total number of days difference between current and previous transaction date. We can use a LAG() function to achieve this.</p>
<p><strong>Below is a full demonstration on this: </strong></p>
<p><strong>The Syntax:</strong></p><pre class="crayon-plain-tag">LAG (scalar_expression [,offset] [,default])
    OVER ( [ partition_by_clause ] order_by_clause )</pre><p>
The default OFFSET value is 1 means considering next immediate row and we can also change this OFFSET value. e.g. set OFFSET value of 4 means it accesses immediate fourth row.</p>
<p><strong>Create a sample table with data:</strong></p><pre class="crayon-plain-tag">CREATE TABLE tbl_LEADDemo
(
	ID INTEGER IDENTITY(1,1)
	,TransactionDateTime DATETIME
)
GO

INSERT INTO tbl_LEADDemo (TransactionDateTime)
VALUES 
('2016-01-08 08:08:08')
,('2016-01-10 14:23:23')
,('2016-01-12 09:41:12')
,('2016-01-16 23:45:12')
,('2016-01-26 16:16:45')
,('2016-01-29 12:55:02')
,('2016-02-02 18:25:04')
,('2016-02-06 16:42:32')
GO</pre><p>
<p><strong>Calculate day difference using LAG():</strong></p><pre class="crayon-plain-tag">;WITH cteLeadDemo AS
(
	SELECT 
		ID
		,TransactionDateTime AS NextDay
		,LAG(TransactionDateTime,1,0) OVER(ORDER BY TransactionDateTime) AS PreviousDay
	FROM tbl_LEADDemo
)
SELECT 
	ID
	,NextDay
	,PreviousDay
	,CASE 
		WHEN PreviousDay = '1900-01-01 00:00:00.000'
			THEN 0 
		ELSE DATEDIFF(DAY,PreviousDay,NextDay) 
	END AS TransactionDayInterval	
FROM cteLeadDemo
GO</pre><p>
<p><strong>The Result:</strong></p>
<p><a href="https://www.dbrnd.com/wp-content/uploads/2016/02/SQLServerLAGFunction.png" rel="attachment wp-att-1512"><img fetchpriority="high" decoding="async" class="alignnone wp-image-1512 size-full" title="SQL Server LAG Function" src="https://www.dbrnd.com/wp-content/uploads/2016/02/SQLServerLAGFunction.png" alt="SQL Server LAG Function" width="419" height="186" srcset="https://www.dbrnd.com/wp-content/uploads/2016/02/SQLServerLAGFunction.png 419w, https://www.dbrnd.com/wp-content/uploads/2016/02/SQLServerLAGFunction-300x133.png 300w, https://www.dbrnd.com/wp-content/uploads/2016/02/SQLServerLAGFunction-150x67.png 150w" sizes="(max-width: 419px) 100vw, 419px" /></a></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2016/02/sql-server-2012-introduced-lag-function-the-best-alternative-of-self-join-to-compare-previous-row/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>SQL Server 2012: Introduced LEAD function, the best alternative of Self Join to compare Next Row</title>
		<link>https://www.dbrnd.com/2016/02/sql-server-2012-introduced-lead-function-the-best-alternative-of-self-join-to-compare-next-row/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Sun, 07 Feb 2016 19:49:00 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[LEAD]]></category>
		<category><![CDATA[Self Join]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server 2012]]></category>
		<category><![CDATA[SQL Server Administrator]]></category>
		<category><![CDATA[SQL Server Monitoring]]></category>
		<category><![CDATA[SQL Server Performance Tunning]]></category>
		<category><![CDATA[SQL Server Tips and Tricks]]></category>
		<category><![CDATA[TSQL]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=1497</guid>

					<description><![CDATA[In this post, I am sharing the demonstration on the new LEAD() function which is introduced by SQL Server 2012. Generally, we are using Self Join to perform row-level analysis by comparing Next and Previous rows. SQL Server 2012 has a LEAD() function, is used for accessing data from the Next Row in the same [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F02%2Fsql-server-2012-introduced-lead-function-the-best-alternative-of-self-join-to-compare-next-row%2F&amp;linkname=SQL%20Server%202012%3A%20Introduced%20LEAD%20function%2C%20the%20best%20alternative%20of%20Self%20Join%20to%20compare%20Next%20Row" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F02%2Fsql-server-2012-introduced-lead-function-the-best-alternative-of-self-join-to-compare-next-row%2F&amp;linkname=SQL%20Server%202012%3A%20Introduced%20LEAD%20function%2C%20the%20best%20alternative%20of%20Self%20Join%20to%20compare%20Next%20Row" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F02%2Fsql-server-2012-introduced-lead-function-the-best-alternative-of-self-join-to-compare-next-row%2F&amp;linkname=SQL%20Server%202012%3A%20Introduced%20LEAD%20function%2C%20the%20best%20alternative%20of%20Self%20Join%20to%20compare%20Next%20Row" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F02%2Fsql-server-2012-introduced-lead-function-the-best-alternative-of-self-join-to-compare-next-row%2F&amp;linkname=SQL%20Server%202012%3A%20Introduced%20LEAD%20function%2C%20the%20best%20alternative%20of%20Self%20Join%20to%20compare%20Next%20Row" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F02%2Fsql-server-2012-introduced-lead-function-the-best-alternative-of-self-join-to-compare-next-row%2F&amp;linkname=SQL%20Server%202012%3A%20Introduced%20LEAD%20function%2C%20the%20best%20alternative%20of%20Self%20Join%20to%20compare%20Next%20Row" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F02%2Fsql-server-2012-introduced-lead-function-the-best-alternative-of-self-join-to-compare-next-row%2F&amp;linkname=SQL%20Server%202012%3A%20Introduced%20LEAD%20function%2C%20the%20best%20alternative%20of%20Self%20Join%20to%20compare%20Next%20Row" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F02%2Fsql-server-2012-introduced-lead-function-the-best-alternative-of-self-join-to-compare-next-row%2F&#038;title=SQL%20Server%202012%3A%20Introduced%20LEAD%20function%2C%20the%20best%20alternative%20of%20Self%20Join%20to%20compare%20Next%20Row" data-a2a-url="https://www.dbrnd.com/2016/02/sql-server-2012-introduced-lead-function-the-best-alternative-of-self-join-to-compare-next-row/" data-a2a-title="SQL Server 2012: Introduced LEAD function, the best alternative of Self Join to compare Next Row"></a></p><p>In this post, I am sharing the demonstration on the new LEAD() function which is introduced by SQL Server 2012.</p>
<blockquote><p>Generally, we are using Self Join to perform row-level analysis by comparing Next and Previous rows.</p>
<p>SQL Server 2012 has a LEAD() function, is used for accessing data from the Next Row in the same result set.</p></blockquote>
<p>For example, we have a different transaction date in our table and need to find the total number of days difference between current and next transaction date. We can use a LEAD() function to accomplish this.</p>
<p><strong>Below is a full demonstration on this: </strong></p>
<p><strong>The Syntax:</strong></p><pre class="crayon-plain-tag">LEAD (scalar_expression [,offset] [,default])
    OVER ( [ partition_by_clause ] order_by_clause )</pre><p>
The default OFFSET value is 1 means considering next immediate row and we can also change this OFFSET value. e.g. set OFFSET value of 4 means it accesses immediate fourth row.</p>
<p><strong>Create a sample table with data:</strong></p><pre class="crayon-plain-tag">CREATE TABLE tbl_LEADDemo
(
	ID INTEGER IDENTITY(1,1)
	,TransactionDateTime DATETIME
)
GO

INSERT INTO tbl_LEADDemo (TransactionDateTime)
VALUES 
('2016-01-08 08:08:08')
,('2016-01-10 14:23:23')
,('2016-01-12 09:41:12')
,('2016-01-16 23:45:12')
,('2016-01-26 16:16:45')
,('2016-01-29 12:55:02')
,('2016-02-02 18:25:04')
,('2016-02-06 16:42:32')
GO</pre><p>
<p><strong>Calculate day difference using LEAD():</strong></p><pre class="crayon-plain-tag">;WITH cteLeadDemo AS
(
	SELECT 
		ID
		,TransactionDateTime AS PreviousDay
		,LEAD(TransactionDateTime,1,0) OVER(ORDER BY TransactionDateTime) AS NextDay
	FROM tbl_LEADDemo
)
SELECT 
	ID
	,PreviousDay
	,NextDay
	,CASE 
	    WHEN DATEDIFF(DAY,PreviousDay,NextDay) &lt; 0 
		THEN 0 
	    ELSE DATEDIFF(DAY,PreviousDay,NextDay) 
	END AS TransactionDayInterval
FROM cteLeadDemo
GO</pre><p>
<p><strong>The Result:</strong></p>
<p><a href="https://www.dbrnd.com/wp-content/uploads/2016/02/SQLServerLEADFunction.png" rel="attachment wp-att-1501"><img decoding="async" class="alignnone wp-image-1501 size-full" title="SQL Server LEAD Function" src="https://www.dbrnd.com/wp-content/uploads/2016/02/SQLServerLEADFunction.png" alt="SQL Server LEAD Function" width="432" height="178" srcset="https://www.dbrnd.com/wp-content/uploads/2016/02/SQLServerLEADFunction.png 432w, https://www.dbrnd.com/wp-content/uploads/2016/02/SQLServerLEADFunction-300x124.png 300w, https://www.dbrnd.com/wp-content/uploads/2016/02/SQLServerLEADFunction-150x62.png 150w" sizes="(max-width: 432px) 100vw, 432px" /></a></p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
