SQL Query-11

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

/* Ranking Functions : Based on the column values it will generate sequential values.

Ranking
Functions are ROW_NUMBER,RANK,DENSE_RANK ROW_NUMBER: It will generate
sequential values for all the rows of a table Syntax: ROW_NUMBER() OVER(PARITITION BY
COLUMNS ORDER BY COLUMNS) */ Select * from Alldatatypes Select *,ROW_NUMBER()
over(order by id) [RN] from Alldatatypes Select *,ROW_NUMBER() over(partition by id order by
id) [RN] from Alldatatypes /* RANK: It will skip the ranking values Syntax: RANK() over(partition
by columns order by columns) */ select * from Alldatatypes select *, RANK() over(order by id)
[RNK] from Alldatatypes /* DENSE_RANK: Without skipping value it will generate sequential
values Syntax: DENSE_RANK() over(partition by columns order by columns) */ select * from
Alldatatypes select *, DENSE_RANK() over(order by id) [DNS_RNK] from Alldatatypes /*
ANALYTICAL Functions are LEAD and LAG LEAD:Same column values starts from which row
LEAD will decide Syntax: LEAD(columnname,rowvalue) over(partition by columns order by
columns) */ select * from Alldatatypes select *,LEAD(id,1) over(order by id) [LEAD] from
Alldatatypes /* LAG: Same column values start's from which row Syntax: LAG(columnname,no
of null values) over(partition by columns order by columns) */ select * from Alldatatypes select
*,LAG(id,2) over(order by id) [LAG] from Alldatatypes /* DATETIME Functions: Datetime
functions are
GETDATE,DATENAME,DATEPART,DAY,MONTH,YEAR,DATEDIFF,DATEADD,EOMONTH,
FORMAT,CAST,CONVERT GETDATE:If we want to generate current date and time use
GETDATE function Syntax: GETDATE() */ Select * from Alldatatypes select *,Getdate()
[CurrentDatetime] from Alldatatypes

You might also like