Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2016 February SQL Server 2012: Introduced LEAD function, the best alternative of Self Join to compare Next Row

SQL Server 2012: Introduced LEAD function, the best alternative of Self Join to compare Next Row

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

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 result set.

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.

Below is a full demonstration on this:

The Syntax:

1
2
LEAD (scalar_expression [,offset] [,default])
OVER ( [ partition_by_clause ] order_by_clause )

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.

Create a sample table with data:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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

Calculate day difference using LEAD():

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
;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) < 0
THEN 0
ELSE DATEDIFF(DAY,PreviousDay,NextDay)
END AS TransactionDayInterval
FROM cteLeadDemo
GO

The Result:

SQL Server LEAD Function

Feb 7, 2016Anvesh Patel
SQL Server: Find First NOT NULL Column using a COALESCE FunctionSQL Server 2012: Introduced LAG function, the best alternative of Self Join to compare Previous Row
Anvesh Patel
Anvesh Patel

Database Engineer

February 7, 2016 SQL ServerAnvesh Patel, database, database research and development, dbrnd, LEAD, Self Join, SQL Query, SQL Server, SQL Server 2012, SQL Server Administrator, SQL Server Monitoring, SQL Server Performance Tunning, SQL Server Tips and Tricks, TSQL
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....