Professional Documents
Culture Documents
PLSQL New Prashant
PLSQL New Prashant
PLSQL New Prashant
Description
The Oracle/PLSQL LAG function is an analytic function that lets you query more than
one row in a table at a time without having to join the table to itself. It returns values
from a previous row in the table. To return a value from the next row, try using
the LEAD function.
Syntax
The syntax for the LAG function in Oracle/PLSQL is:
Parameters or Arguments
expression
An expression that can contain other built-in functions, but can not contain
any analytic functions.
offset
Optional. It is the physical offset from the current row in the table. If this
parameter is omitted, the default is 1.
default
Optional. It is the value that is returned if the offset goes out of the bounds of
the table. If this parameter is omitted, the default is null.
query_partition_clause
Optional. It is used to partition the results into groups based on one or more
expressions.
order_by_clause
Optional. It is used to order the data within each partition.
Example
The LAG function can be used in Oracle/PLSQL.
Let's look at an example. If we had an orders table that contained the following data:
PRODUCT_I
ORDER_DATE QTY
D
2007/09/25 1000 20
2007/09/26 2000 15
2007/09/27 1000 8
2007/09/28 2000 12
2007/09/29 2000 2
2007/09/30 1000 4
PRODUCT_I
ORDER_DATE PREV_ORDER_DATE
D
Using Partitions
Now let's look at a more complex example where we use a query partition clause to
return the previous order_date for each product_id.
PRODUCT_I
ORDER_DATE PREV_ORDER_DATE
D
In this example, the LAG function will partition the results by product_id and then sort
by order_date as indicated by PARTITION BY product_id ORDER BY order_date. This
means that the LAG function will only evaluate an order_date value if
the product_id matches the current record's product_id. When a newproduct_id is
encountered, the LAG function will restart its calculations and use the
appropriate product_id partition.
As you can see, the 1st record in the result set has a value of NULL for
the prev_order_date because it is the first record for the partition where product_id is
1000 (sorted by order_date) so there is no lower order_date value. This is also true
for the 4th record where the product_id is 2000.
Oracle/PLSQL: LEAD Function
This Oracle tutorial explains how to use the Oracle/PLSQL LEAD function with
syntax and examples.
Description
The Oracle/PLSQL LEAD function is an analytic function that lets you query more
than one row in a table at a time without having to join the table to itself. It returns
values from the next row in the table. To return a value from a previous row, try using
the LAG function.
Syntax
The syntax for the LEAD function in Oracle/PLSQL is:
Parameters or Arguments
expression
An expression that can contain other built-in functions, but can not contain
any analytic functions.
offset
Optional. It is the physical offset from the current row in the table. If this
parameter is omitted, the default is 1.
default
Optional. It is the value that is returned if the offset goes out of the bounds of
the table. If this parameter is omitted, the default is null.
query_partition_clause
Optional. It is used to partition the results into groups based on one or more
expressions.
order_by_clause
Optional. It is used to order the data within each partition.
Example
The LEAD function can be used in Oracle/PLSQL.
Let's look at an example. If we had an orders table that contained the following data:
PRODUCT_I
ORDER_DATE QTY
D
2007/09/25 1000 20
2007/09/26 2000 15
2007/09/27 1000 8
2007/09/28 2000 12
2007/09/29 2000 2
2007/09/30 1000 4
PRODUCT_I
ORDER_DATE NEXT_ORDER_DATE
D
Using Partitions
Now let's look at a more complex example where we use a query partition clause to
return the next order_date for each product_id.
PRODUCT_I
ORDER_DATE NEXT_ORDER_DATE
D
In this example, the LEAD function will partition the results by product_id and then
sort by order_date as indicated by PARTITION BY product_id ORDER BY order_date.
This means that the LEAD function will only evaluate an order_date value if
the product_id matches the current record's product_id. When a newproduct_id is
encountered, the LEAD function will restart its calculations and use the
appropriate product_id partition.
As you can see, the 3rd record in the result set has a value of NULL for
the next_order_date because it is the last record for the partition where product_id is
1000 (sorted by order_date). This is also true for the 6th record where
the product_id is 2000.
Oracle/PLSQL: RANK Function
This Oracle tutorial explains how to use the Oracle/PLSQL RANK function with
syntax and examples.
Description
The Oracle/PLSQL RANK function returns the rank of a value in a group of values. It
is very similar to the DENSE_RANK function. However, the rank function can cause
non-consecutive rankings if the tested values are the same. Whereas,
the DENSE_RANK function will always result in consecutive rankings.
Parameters or Arguments
expr1
First expression which identifies a unique row in the group.
expr2, ... expr_n
Optional. Additional expressions which identifies a unique row in the group.
Note
There must be the same number of expressions in the first expression list as
there is in the ORDER BY clause.
The expression lists match by position so the data types must be compatible
between the expressions in the first expression list as in the ORDER BY
clause.
Applies To
The RANK function can be used in the following versions of Oracle/PLSQL:
For example:
The SQL statement above would return the rank of an employee with a salary of
$1,000 and a bonus of $500 from within the employees table.
Applies To
The RANK function can be used in the following versions of Oracle/PLSQL:
Description
The Oracle/PLSQL DENSE_RANK function returns the rank of a row in a group of
rows. It is very similar to the RANK function. However, the RANK function can cause
non-consecutive rankings if the tested values are the same. Whereas, the
DENSE_RANK function will always result in consecutive rankings.
The syntax for the DENSE_RANK function when used as an Aggregate function is:
Parameters or Arguments
expression1 .. expression_n
One or more expressions which identify a unique row in the group.
Note
There must be the same number of expressions in the first expression list as
there is in the ORDER BY clause.
The expression lists match by position so the data types must be compatible
between the expressions in the first expression list as in the ORDER BY
clause.
Applies To
The DENSE_RANK function can be used in the following versions of Oracle/PLSQL:
Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i
For example:
The SQL statement above would return the dense rank of an employee with a salary
of $1,000 and a bonus of $500 from within the employees table.
The syntax for the DENSE_RANK function when used as an Analytic function is:
Applies To
The DENSE_RANK function can be used in the following versions of Oracle/PLSQL:
For example:
Here’s a quick summary of OVER and PARTITION BY (new in SQL 2005), for the
uninitiated or forgetful…
OVER
OVER allows you to get aggregate information without using a GROUP BY. In other words,
you can retrieve detail rows, and get aggregate data alongside it. For example, this query:
Cost OrderNum
10.00 345
10.00 346
10.00 347
10.00 348
Quick translation:
OVER(PARTITION BY)
OVER, as used in our previous example, exposes the entire resultset to the
aggregation…”Cost” was the sum of all [Cost] in the resultset. We can break up that
resultset into partitions with the use of PARTITION BY:
SELECT SUM(Cost) OVER (PARTITION BY CustomerNo) AS Cost
, OrderNum
, CustomerNo
FROM Orders
My partition is by CustomerNo – each “window” of a single customer’s orders will be
treated separately from each other “window”….I’ll get the sum of cost for Customer 1, and
then the sum for Customer 2:
Cost OrderNum CustomerNo
8.00 345 1
8.00 346 1
8.00 347 1
2.00 348 2
The translation here is:
Note that this query also will not give multiple records if there are
employees with same salaries ( compare query (2) and (4) )