Oracle Reporting 5 - Windowing
Winodwing clause : rows | range between ... and ....
ROWS - specifies the window in physical units (rows). For windows expressed in rows, the ordering expressions should be unique to produce deterministic results.
RANGE - specifies the window as a logical offset. A logical offset can be specified with constants such as RANGE 10 PRECEDING, or an expression that evaluates to a constant, or by an interval specification like RANGE INTERVAL N DAY/MONTH/YEAR PRECEDING or an expression that evaluates to an interval.
If you omit the windowing_clause entirely, the default is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW. The sum() over(partition by xxx order by yyyy) therefore is a cumulative aggregation.
If you omit BETWEEN and specify only one end point, then Oracle considers it the start point, and the end point defaults to the current row.
UNBOUNDED PRECEDING: to indicate that the window starts at the first row of the partition.
UNBOUNDED FOLLOWING: to indicate that the window ends at the last row of the partition.
Query 1 - cumulative aggregation using sum()
select deptno,ename,sal,
sum(sal) over(partition by deptno order by empno) cumulative_sal
from emp
order by deptno;
OR
select deptno,ename,sal,
sum(sal) over(partition by deptno order by empno
RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) cumulative_sal
from emp
order by deptno;
Query 2 - moving aggregation
SELECT c.cust_id, t.calendar_month_desc,
TO_CHAR (SUM(amount_sold),'9,999,999,999') AS SALES,
TO_CHAR(AVG(SUM(amount_sold)) OVER (ORDER BY c.cust_id, t.calendar_month_desc ROWS 2 PRECEDING), '9,999,999,999') AS MOVING_3_MONTH_AVG
FROM sales s, times t, customers c
WHERE s.time_id=t.time_id AND s.cust_id=c.cust_id
AND t.calendar_year=1999 AND c.cust_id IN (6510)
GROUP BY c.cust_id, t.calendar_month_desc
ORDER BY c.cust_id, t.calendar_month_desc;
This query calculates the average sales of last 3 month.
Query 3 – Centered calculation using range and interval.
SELECT t.time_id, TO_CHAR (SUM(amount_sold), '9,999,999,999') AS SALES,
AVG(SUM(amount_sold)) OVER (ORDER BY t.time_id RANGE BETWEEN INTERVAL '1' DAY PRECEDING AND INTERVAL '1' DAY FOLLOWING) AS CENTERED_3_DAY_AVG
FROM sales s, times t
WHERE s.time_id=t.time_id AND t.calendar_week_number IN (51) AND calendar_year=1999
GROUP BY t.time_id
ORDER BY t.time_id;
Query 4 - Varying window size for each row by using a function.
The following statement for a hypothetical stock price database uses a user-defined function in its RANGE clause to set window size:
SELECT t_timekey, AVG(stock_price)
OVER (ORDER BY t_timekey RANGE fn(t_timekey) PRECEDING) av_price
FROM stock, time WHERE st_timekey = t_timekey
ORDER BY t_timekey;
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/638844/viewspace-1061137/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Error reporting for dbusError
- PostgreSQL DBA(62) - PG 12 More progress reportingSQL
- DevExpress v17.2新版亮點—.NET Reporting篇(二)devExpress
- DevExpress v17.2新版亮點—.NET Reporting篇(一)devExpress
- Microsoft Sql Server 2017 Reporting Services 不支援安裝在DC上ROSSQLServer
- Oracle和MySQL md5加密OracleMySql加密
- Oracle中的MD5加密Oracle加密
- Dynamics CRM CRM Reporting Error: Error occurred while fetching the data extension的解決方法ErrorWhile
- 5:安裝配置 Oracle18COracle
- 在FC5上安裝oracle 9204Oracle
- 5分鐘,徹底精通Oracle DG切換Oracle
- 5. Oracle連線和使用——5.1. sqlplusOracleSQL
- 5. Oracle連線和使用——5.2. PL/SQL DeveloperOracleSQLDeveloper
- Oracle高階培訓 第5課 學習筆記Oracle筆記
- Oracle vs PostgreSQL,研發注意事項(5)- 字元型別OracleSQL字元型別
- 5分鐘,輕鬆搞定Oracle資料庫等保測評Oracle資料庫
- oracle orion 壓測磁碟效能raid10 raid5 raid50 資料OracleAI
- Oracle分割槽表基礎運維-07增加分割槽(5RANGE_LIST)Oracle運維
- ORACLE基礎之oracle鎖(oracle lock mode)詳解Oracle
- cx_Oracle 連線 OracleOracle
- 【Oracle】Oracle logminer功能介紹Oracle
- [Oracle]Oracle良性SQL建議OracleSQL
- OracleOracle
- 「Oracle」Oracle 資料庫安裝Oracle資料庫
- Oracle vs PostgreSQL DBA(21)- Oracle VPDOracleSQL
- [Oracle] -- 配置Oracle環境變數Oracle變數
- Oracle案例12——NBU Oracle恢復Oracle
- Oracle案例13—— OGG-01163 Oracle GoldenGate Delivery for Oracle, reprpt01.prmOracleGo
- oracle資料庫與oracle例項Oracle資料庫
- 「Oracle」Oracle 資料庫基本概念Oracle資料庫
- 「Oracle」Oracle高階查詢介紹Oracle
- 「Oracle」Oracle資料庫基本概念Oracle資料庫
- 4.1.6 Oracle Restart 與 Oracle Data Guard 整合OracleREST
- Oracle 阻塞Oracle
- ORACLE AWROracle
- oracle程序Oracle
- oracle 文件Oracle
- Oracle 索引Oracle索引
- Oracle:RBOOracle