Feeds:
Posts
Comments

Archive for November, 2012

In my previous article, I discussed about LAG. Today, we will discuss another analytical function namely“LEAD” in SQL SERVER 2012. This function performs exactly opposite functionality of LAG. In simple words we can say that if we need to pick up any value from the next row(s) of the current row, we can use this function and get it, instead of using self joins.

Let me explain its syntax, parameters, purpose and examples in detail.
Syntax :

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

Parameters :

@scalar_expression :The value to be returned based on the offset. (Mandatory)
@offset : The number of rows ahead of the rows from where we need to pick the value. Default value is 1 (Optional)
@default : If @scalar_expression is NULL, then you can define any default value here to be returned (Optional)

Purpose :
The purpose of this function is to get the next row in front of the current row in the same result without using any self joins. And once you get the next row parallel to current, you can easily compare their values and perform any analytical task.

Lets create an example to explain “LEAD” function in SQL SERVER 2012.

USE tempdb
GO
Create Table [Test_Table]
(
[S.No] int,
[Letters] varchar(50)
)
GO
Insert into [Test_Table] values (1,'LETTER A')
Insert into [Test_Table] values (2,'LETTER B')
Insert into [Test_Table] values (3,'LETTER C')
Insert into [Test_Table] values (5,'LETTER D')
Insert into [Test_Table] values (6,'LETTER E')
Insert into [Test_Table] values (7,'LETTER F')
Insert into [Test_Table] values (8,'LETTER G')
Insert into [Test_Table] values (9,'LETTER H')
Insert into [Test_Table] values (10,'LETTER I')

Let me explain this with simple examples.

Example-1 : LEAD- WITH DEFAULT VALUES

Select [S.No],[Letters]
,LEAD([Letters]) Over (Order By [Letters]) as [Next Value]
from  [Test_Table]

Example-2 : LEAD – REPLACE DEFAULT VALUE NULL WITH EMPTY STRING
In the above example, you can view that if LEAD could not find the value it gives you NULL. In order to avoid NULL, you can pass the third parameter and it will be replaced by NULL.
In the example given below, I replaced NULL with empty spaces (”).

Select [S.No],[Letters]
,LEAD([Letters],1,'') Over (Order By [Letters]) as [Next Value]
from  [Test_Table]

Example-3 : LEAD – CHANGE OFFSET VALUE FROM DEFAULT TO 2
In the above examples, you can view that LEAD function picks one row subsequent value than current row.
In this example, we will set the offset value to 2 to pick up two rows’ next value than current.

Select [S.No],[Letters]
,LEAD([Letters],2,'') Over (Order By [Letters]) as [Next Value]
from  [Test_Table]

Example-4 : LEAD – REAL WORLD – FIND Promotions of Employees
In the earlier version of SQL SERVER, if you need to find current designation with the promotion of any employee in the same result set , you need to use the self join to achieve it. But in SQL SERVER 2012, you can use the LEAD function to achieve it.

Lets create an example to explain this.

Use tempdb
Create table [tbl_Promotion]
(
[S.No] int,
[Date] datetime,
[Employee Name] varchar(50),
[Designation]  varchar(50)
)
 
Insert into [tbl_Promotion] values(1,'2009-08-20','Imran','Assistant Manager')
Insert into [tbl_Promotion] values(2,'2011-11-21','Imran','Manager')
 
Insert into [tbl_Promotion] values(3,'2010-09-05','Bob','Technical Manager')
Insert into [tbl_Promotion] values(4,'2012-10-06','Bob','Technical Director')
 
Insert into [tbl_Promotion] values(5,'2012-01-10','Robert','Junior Developer')
Insert into [tbl_Promotion] values(6,'2012-06-11','Robert','Developer')
 
GO
Select * from [tbl_Promotion]
 
GO
 
Select [S.No],[Date],[Employee Name],[Designation]
,LEAD ([Designation],1,0) Over (Partition By [Employee Name] Order By [S.No]) as [Next Promotion]
from [tbl_Promotion]
Go
Drop Table [tbl_Promotion]

Reference :MSDN

Read Full Post »

Today, we will discuss one of the important analytical functions namely “LAG” in SQL SERVER 2012. In simple words we can say that if we need to pick up any value from the previous rows in the current row, we can use this function and get it, instead of using self joins.

Lets discuss each and every aspect of this function. Also we will discuss the need/importance of this function in SQL server.

By using this function, we can reduce the number of codes because in the previous versions of SQL SERVER, to do the same, we had to use self joins to achieve it.

Let me explain its syntax, parameters, purpose and examples in detail.
Syntax :

      LAG (scalar_expression [,offset] [,default])
    OVER ( [ partition_by_clause ] order_by_clause )

Parameters :

@scalar_expression :The value to returned based on the offset. (Mandatory)
@offset : The number of rows back from the rows from where we need to pick the value. Default value is 1 (Optional)
@default : If @scalar_expression is NULL, then you can define any default value here to be returned.(Optional)

Purpose :
The purpose of this function is to get the previous row next to current row in the same result without using any self joins. And once you will get the previous row parallel to current, you can easily compare their values and perform any analytical task.

Lets create an example to explain “LAG” function in SQL SERVER 2012.

USE tempdb 
GO
Create Table [Test_Table]
(
[S.No] int,
[Letters] varchar(50)
)
GO
Insert into [Test_Table] values (1,'LETTER A')
Insert into [Test_Table] values (2,'LETTER B')
Insert into [Test_Table] values (3,'LETTER C')
Insert into [Test_Table] values (5,'LETTER D')
Insert into [Test_Table] values (6,'LETTER E')
Insert into [Test_Table] values (7,'LETTER F')
Insert into [Test_Table] values (8,'LETTER G')
Insert into [Test_Table] values (9,'LETTER H')
Insert into [Test_Table] values (10,'LETTER I')

Let me explain this with simple examples.

Example-1 : LAG – WITH DEFAULT VALUES

Select [S.No],[Letters]
,LAG([Letters]) Over (Order By [Letters]) as [Previous Value]
from  [Test_Table]

Example-2 : LAG – REPLACE DEFAULT VALUE NULL WITH EMPTY STRING
In the above example, you can view that if LAG could not find the value it gives you NULL. In order to avoid NULL, you can pass the third parameter and it will be replaced by NULL.
Given below example, I replaced NULL with empty spaces (”).

Select [S.No],[Letters]
,LAG([Letters],1,'') Over (Order By [Letters]) as [Previous Value]
from  [Test_Table]

Example-3 : LAG – CHANGE OFFSET VALUE FROM DEFAULT OF 2
In the above examples, you can view that LAG function picks one row previous value than current row.
In this example, we will set the offset value to 2 to pick up two rows previous value than current.

Select [S.No],[Letters]
,LAG([Letters],2,'') Over (Order By [Letters]) as [Previous Value]
from  [Test_Table]

Example-4 : LAG – REAL WORLD – FIND PREVIOUS DAY EXCHANGE RATE
In the earlier version of SQL SERVER, if you need to find previous day exchange rate, you need to use the self join to achieve it. But in SQL SERVER 2012, you can use the LAG function to achieve it.

Lets create an example to explain this.

Use tempdb
GO
Create table [Daily_Currency_Rate]
(
[S.No] int,
[Date] datetime,
[Currency] varchar(50),
[Exchange Rate] numeric(18,4)
)

Insert into [Daily_Currency_Rate] values(1,'2012-11-20','Indian Rupee (INR)',14.86)
Insert into [Daily_Currency_Rate] values(2,'2012-11-21','Indian Rupee (INR)',14.87)

Insert into [Daily_Currency_Rate] values(3,'2012-11-20','Pakistani Rupee (PKR)',25.84)
Insert into [Daily_Currency_Rate] values(4,'2012-11-21','Pakistani Rupee (PKR)',25.83)

Insert into [Daily_Currency_Rate] values(5,'2012-11-20','Sri Lankan Rupee (LKR)',35.38)
Insert into [Daily_Currency_Rate] values(6,'2012-11-21','Sri Lankan Rupee (LKR)',35.44)

GO
Select * from [Daily_Currency_Rate]

GO

Select [S.No],[Date],[Currency],[Exchange Rate]
,LAG([Exchange Rate],1,0) Over (Partition By [Currency] Order By [Currency]) as [Yesterday Rate]
from [Daily_Currency_Rate]

GO
Drop Table [Daily_Currency_Rate]

Reference :MSDN

Read Full Post »