SUM(y) OVER(ORDER BY x ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) : Aggregrate Analytical « Analytical Functions « Oracle PL / SQL

Oracle PL / SQL
1. Aggregate Functions
2. Analytical Functions
3. Char Functions
4. Constraints
5. Conversion Functions
6. Cursor
7. Data Type
8. Date Timezone
9. Hierarchical Query
10. Index
11. Insert Delete Update
12. Large Objects
13. Numeric Math Functions
14. Object Oriented Database
15. PL SQL
16. Regular Expressions
17. Report Column Page
18. Result Set
19. Select Query
20. Sequence
21. SQL Plus
22. Stored Procedure Function
23. Subquery
24. System Packages
25. System Tables Views
26. Table
27. Table Joins
28. Trigger
29. User Previliege
30. View
31. XML
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Oracle PL / SQL » Analytical Functions » Aggregrate Analytical 
SUM(y) OVER(ORDER BY x ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING)



SQL> create table TestTable (
  2    x    number primary key,
  3    y   number
  4  );

Table created.

SQL> insert into TestTable values (1);

row created.

SQL> insert into TestTable values (2);

row created.

SQL> insert into TestTable values (3);

row created.

SQL> insert into TestTable values (4);

row created.

SQL> insert into TestTable values (5);

row created.

SQL> insert into TestTable values (634 );

row created.

SQL> insert into TestTable values (732 );

row created.

SQL> insert into TestTable values (843 );

row created.

SQL> insert into TestTable values (987 );

row created.

SQL> insert into TestTable values (1032 );

row created.

SQL> insert into TestTable values (1112 );

row created.

SQL> insert into TestTable values (1216 );

row created.

SQL> insert into TestTable values (1363 );

row created.

SQL> insert into TestTable values (1474 );

row created.

SQL> insert into TestTable values (1536 );

row created.

SQL> insert into TestTable values (1656 );

row created.

SQL> insert into TestTable values (17);

row created.

SQL>
SQL> select from TestTable;

         X          Y
---------- ----------
         1          7
         2          1
         3          2
         4          5
         5          7
         6         34
         7         32
         8         43
         9         87
        10         32
        11         12
        12         16
        13         63
        14         74
        15         36
        16         56
        17          2

17 rows selected.

SQL>
SQL>
SQL>
SQL> COLUMN ma FORMAT 99.999
SQL> COLUMN sum LIKE ma
SQL> COLUMN "sum/3" LIKE ma
SQL>
SQL> SELECT x, y,
  2    AVG(yOVER(ORDER BY x
  3      ROWS BETWEEN PRECEDING AND FOLLOWINGma,
  4    SUM(yOVER(ORDER BY x
  5      ROWS BETWEEN PRECEDING AND FOLLOWINGsum,
  6    (SUM(yOVER(ORDER BY y
  7      ROWS BETWEEN PRECEDING AND FOLLOWING))/"Sum/3"
  8  FROM TestTable
  9  ORDER BY x;

         X          Y      MA     SUM   Sum/3
---------- ---------- ------- ------- -------
         1          7   4.000   8.000   6.333
         2          1   3.333  10.000   1.000
         3          2   2.667   8.000   1.667
         4          5   4.667  14.000   4.667
         5          7  15.333  46.000   8.667
         6         34  24.333  73.000  34.000
         7         32  36.333 #######  26.667
         8         43  54.000 #######  45.000
         9         87  54.000 #######  53.667
        10         32  43.667 #######  32.667
        11         12  20.000  60.000  11.667
        12         16  30.333  91.000  20.000
        13         63  51.000 #######  64.333
        14         74  57.667 #######  74.667
        15         36  55.333 #######  37.667
        16         56  31.333  94.000  54.000
        17          2  29.000  58.000   3.000

17 rows selected.

SQL>
SQL> COLUMN ma FORMAT 999999.999
SQL> COLUMN sum LIKE ma
SQL> COLUMN "sum/3" LIKE ma
SQL>
SQL>
SQL> SELECT x, y,
  2    AVG(yOVER(ORDER BY x
  3      ROWS BETWEEN PRECEDING AND FOLLOWINGma,
  4    SUM(yOVER(ORDER BY x
  5      ROWS BETWEEN PRECEDING AND FOLLOWINGsum,
  6    (SUM(yOVER(ORDER BY y
  7      ROWS BETWEEN PRECEDING AND FOLLOWING))/"Sum/3"
  8  FROM TestTable
  9  ORDER BY x;

         X          Y          MA         SUM       Sum/3
---------- ---------- ----------- ----------- -----------
         1          7       4.000       8.000       6.333
         2          1       3.333      10.000       1.000
         3          2       2.667       8.000       1.667
         4          5       4.667      14.000       4.667
         5          7      15.333      46.000       8.667
         6         34      24.333      73.000      34.000
         7         32      36.333     109.000      26.667
         8         43      54.000     162.000      45.000
         9         87      54.000     162.000      53.667
        10         32      43.667     131.000      32.667
        11         12      20.000      60.000      11.667
        12         16      30.333      91.000      20.000
        13         63      51.000     153.000      64.333
        14         74      57.667     173.000      74.667
        15         36      55.333     166.000      37.667
        16         56      31.333      94.000      54.000
        17          2      29.000      58.000       3.000

17 rows selected.

SQL>
SQL> drop table TestTable;

Table dropped.

SQL>
           
       
Related examples in the same category
1. count(*) over partition, order by, range unbounded preceding
2. count(*) over partition by, order by, range unbounded preceding
3. Employee salary report with avg salary for the previous 12 months
4. avg over range between
5. Is our average total_order_price increasing or decreasing?
6. analytic order-by clause
7. avg over and avg over order by
8. Sum over order by
9. Sum over partition by and order by
10. avg over order by range
11. average 5 before, after
12. Row-ordering is done first and then the moving average
13. Avg over ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING)
14. Use the COUNT aggregate analytical function to show how many rows are included in each window
15. To see how the moving average window can expand
16. Uses dates and logical offset of seven days preceding
17. A seven-day MAX and MIN on Tuesdays
18. A seven-day MAX and MIN on Tuesdays: using TO_CHAR function
19. Displaying a Running Total Using SUM as an Analytical Function
20. Reporting on a Sum
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.