Analytic Function: Number Real
Analytic Function: Number Real
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:
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:
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