Вы находитесь на странице: 1из 3

Analytic Function

Analytic_Function(<Any-Legal-Expression usually just a column>)


over (
<Partition-By-Clause>
<Order-By-Clause>
<Window-Clause>
)

Analytic functions operate on subsets of rows, similar to aggregate functions in


GROUP BY queries, but they do not reduce the number of rows returned by the query

The real key is the OVER expression. You see it contains a PARTITION BY clause, an
ORDER BY clause, and a WINDOW clause. Forget about the window clause for now; it is
not used most of the time anyway. If you really want to know why it exists, think
about what a smoothed average is. Removing the WINDOW clause leaves us with this
form:

Analytic_Fuction(<Any-Legal-Expression usually just a column>)


over (
<Partition By Clause>
<Order By Clause>
)

Partition By Clause
The Partition By Clause divides the result set into partitions, or groups, of
data. The operation of the analytic function is restricted to the boundary
imposed by these partitions, similar to the way a GROUP BY clause affects the
action of an aggregate function. If the Partition By Clause is omitted, the
whole result set is treated as a single partition.

Order By Clause
The Order By Clause is used to order rows, or siblings, within a partition.
So if an analytic function is sensitive to the order of the siblings in a
partition you should include an Order By Clause.
Remember, the conventional ORDER BY clause is performed after the analytic
processing, so it will always take precedence.

e.g.
SUM (amount) OVER (PARTITION BY account ORDER BY entry_date) AS balance

Example:

CREATE TABLE xxdi_test


(
account CHAR (4)
,entry_date DATE
,amount NUMBER (4)
,balance NUMBER (4)
)

INSERT INTO xxdi_test


VALUES ('1000'
,'01-jan-2000'
,'50'
,NULL);
INSERT INTO xxdi_test
Analytic Function

VALUES ('1000'
,'02-jan-2000'
,'40'
,NULL);
INSERT INTO xxdi_test
VALUES ('1000'
,'03-jan-2000'
,'-50'
,NULL);
INSERT INTO xxdi_test
VALUES ('1000'
,'04-jan-2000'
,'10'
,NULL);
INSERT INTO xxdi_test
VALUES ('1000'
,'05-jan-2000'
,'-250'
,NULL);
INSERT INTO xxdi_test
VALUES ('1000'
,'06-jan-2000'
,'100'
,NULL);
INSERT INTO xxdi_test
VALUES ('1000'
,'07-jan-2000'
,'50'
,NULL);
INSERT INTO xxdi_test
VALUES ('2000'
,'01-jan-2000'
,'30'
,NULL);
INSERT INTO xxdi_test
VALUES ('2000'
,'02-jan-2000'
,'10'
,NULL);
INSERT INTO xxdi_test
VALUES ('2000'
,'03-jan-2000'
,'-520'
,NULL);
INSERT INTO xxdi_test
VALUES ('2000'
,'04-jan-2000'
,'140'
,NULL);
INSERT INTO xxdi_test
VALUES ('2000'
,'05-jan-2000'
,'-4'
,NULL);
INSERT INTO xxdi_test
VALUES ('2000'
,'06-jan-2000'
Analytic Function

,'120'
,NULL);
INSERT INTO xxdi_test
VALUES ('2000'
,'07-jan-2000'
,'57'
,NULL);

SELECT account
,entry_date
,amount
,SUM (amount) OVER (PARTITION BY account ORDER BY entry_date) AS balance
FROM xxpr_test1
ORDER BY account
,entry_date

After analyzing the result and understanding kindly drop the test table
created

DROP TABLE xxdi_test

Вам также может понравиться