LEAD Function in SQL Server |
In LEAD function partition_by_clause is optional, order_by_clause is required.You can specify any number of columns in the order_by_clause. The offset denotes the number of rows to lead. The default_value to return if the number of rows to lead goes beyond first row in a table or partition. If the default_value is not specified NULL is returned.
In order to achieve this we are going to use "tbl_EmployeeDetails" table for our demo.
So our table will look like this,
Now, Lets see how to use LEAD function in a SELECT statement,
Notice that we haven't specified the offset and default value in the above statement. If you don't specify the offset by default it will take as 1, If you don't specify the default_value it will return as NULL like you can see in the below result set.
Suppose, If you want to lead the result set based on the descending order of the Id, your query should be like this:
The returned result is,
In order to lead with different offset and default value. your SELECT statement should be like this:
In above statement the value 2 denotes the lead offset it will lead two rows from the current row. The 0 indicates the default value.
The statement returned result is,
Now, Lets see how to use partition_by_clause in LEAD function?
In order to achieve this we have included department in the employees table based on the department we are going to partition the result set.
The returned result is,
The partition_by_clause divides our department and produces the LeadSalary based on the offset and default value.
In order to achieve this we are going to use "tbl_EmployeeDetails" table for our demo.
CREATE TABLE [dbo].[tbl_EmployeeDetails](
[Id] [bigint] IDENTITY(1,1) NOT NULL,
[Employee] [varchar](450) NULL,
[Salary] [decimal](18, 0) NULL,
CONSTRAINT [PK_tbl_EmployeeDetails] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
-- insert some sample data
insert into tbl_EmployeeDetails(Employee,Salary) values('Michael',25000);
insert into tbl_EmployeeDetails(Employee,Salary) values('Stuart',14000);
insert into tbl_EmployeeDetails(Employee,Salary) values('Stella',28000);
insert into tbl_EmployeeDetails(Employee,Salary) values('Kennedy',30000);
insert into tbl_EmployeeDetails(Employee,Salary) values('Dona Thomas',26000);
insert into tbl_EmployeeDetails(Employee,Salary) values('Kraig',32000);
[Id] [bigint] IDENTITY(1,1) NOT NULL,
[Employee] [varchar](450) NULL,
[Salary] [decimal](18, 0) NULL,
CONSTRAINT [PK_tbl_EmployeeDetails] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
-- insert some sample data
insert into tbl_EmployeeDetails(Employee,Salary) values('Michael',25000);
insert into tbl_EmployeeDetails(Employee,Salary) values('Stuart',14000);
insert into tbl_EmployeeDetails(Employee,Salary) values('Stella',28000);
insert into tbl_EmployeeDetails(Employee,Salary) values('Kennedy',30000);
insert into tbl_EmployeeDetails(Employee,Salary) values('Dona Thomas',26000);
insert into tbl_EmployeeDetails(Employee,Salary) values('Kraig',32000);
Now, Lets see how to use LEAD function in a SELECT statement,
Notice that we haven't specified the offset and default value in the above statement. If you don't specify the offset by default it will take as 1, If you don't specify the default_value it will return as NULL like you can see in the below result set.
Suppose, If you want to lead the result set based on the descending order of the Id, your query should be like this:
The returned result is,
In order to lead with different offset and default value. your SELECT statement should be like this:
In above statement the value 2 denotes the lead offset it will lead two rows from the current row. The 0 indicates the default value.
The statement returned result is,
Now, Lets see how to use partition_by_clause in LEAD function?
In order to achieve this we have included department in the employees table based on the department we are going to partition the result set.
The partition_by_clause divides our department and produces the LeadSalary based on the offset and default value.
Comments