Oracle Reporting 2 - Subtotals and Grand Total
Oracle provides rollup, cube, and grouping sets extensions to group to calculate subtotals and grandtotals. Each one fits into different scenarios. To start with, let's take a look at the following query.
select channel_id, sum(amount_sold) amount_sold
from sales
group by channel_id;
What if I want the grant total as the last row?
select decode(grouping(channel_id), 1, 'Grand-Total', to_char(channel_id)) channel_id,
sum(amount_sold) amount_sold
from sales
group by rollup(channel_id);
In this query, I used grouping() function and rollup extension to group by.
Grouping
Using a single column as its argument, GROUPING returns 1 when it encounters a NULL value created by a ROLLUP or CUBE operation. That is, if the NULL indicates the row is a subtotal, GROUPING returns a 1. Any other type of value, including a stored NULL, returns a 0.
Rollup
ROLLUP enables a SELECT statement to calculate multiple levels of subtotals across a specified group of dimensions. It also calculates a grand total. To further illustrate the rollup extension. Let's examine this query:
select deptno, job, sum(sal)
from emp
group by rollup(deptno, job);
In here, there're three levels of aggregations:
Group by deptno, job
Group by deptno
Grand total.
Keep in mind that the order of columns in the group by extension does matter. See:
select deptno, job, sum(sal)
from emp
group by rollup(job, deptno);
The aggregations here are - group by job, deptno, group by job and grand total. Now, let's move on to grouping sets. With grouping sets, user is able to specify the aggregation levels.
select deptno, job, sum(sal)
from emp
group by grouping sets((job, deptno), (job), (deptno))
order by deptno;
The aggreagations here are: group by (job, deptno), group by job, group by deptno. And here's no grand total. Oracle calculates the aggregations specified in the grouping sets extension, no more and no less.
Finally, let's take a look at cube extension:
select deptno, job, sum(sal)
from emp
group by cube(deptno, job)
order by deptno;
If user specifies cube, Oracle calculates all possible aggregations. Here, they're group by (job, deptno), group by job, group by deptno and grand total.
select channel_id, sum(amount_sold) amount_sold
from sales
group by channel_id;
What if I want the grant total as the last row?
select decode(grouping(channel_id), 1, 'Grand-Total', to_char(channel_id)) channel_id,
sum(amount_sold) amount_sold
from sales
group by rollup(channel_id);
Grouping
Using a single column as its argument, GROUPING returns 1 when it encounters a NULL value created by a ROLLUP or CUBE operation. That is, if the NULL indicates the row is a subtotal, GROUPING returns a 1. Any other type of value, including a stored NULL, returns a 0.
Rollup
ROLLUP enables a SELECT statement to calculate multiple levels of subtotals across a specified group of dimensions. It also calculates a grand total. To further illustrate the rollup extension. Let's examine this query:
select deptno, job, sum(sal)
from emp
group by rollup(deptno, job);
Group by deptno, job
Group by deptno
Grand total.
Keep in mind that the order of columns in the group by extension does matter. See:
select deptno, job, sum(sal)
from emp
group by rollup(job, deptno);
The aggregations here are - group by job, deptno, group by job and grand total. Now, let's move on to grouping sets. With grouping sets, user is able to specify the aggregation levels.
select deptno, job, sum(sal)
from emp
group by grouping sets((job, deptno), (job), (deptno))
order by deptno;
The aggreagations here are: group by (job, deptno), group by job, group by deptno. And here's no grand total. Oracle calculates the aggregations specified in the grouping sets extension, no more and no less.
Finally, let's take a look at cube extension:
select deptno, job, sum(sal)
from emp
group by cube(deptno, job)
order by deptno;
If user specifies cube, Oracle calculates all possible aggregations. Here, they're group by (job, deptno), group by job, group by deptno and grand total.
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/638844/viewspace-777448/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Oracle Reporting 6 - ModelOracle
- Flashback Data Archive (Oracle Total Recall)HiveOracle
- Oracle Reporting 5 - WindowingOracle
- Oracle Reporting 7 - Model ExamplesOracle
- Oracle Reporting 4 - Time Series CalculationsOracle
- How To Using Flashback Data Archive (Oracle Total Recall)HiveOracle
- Oracle Reporting 3 - Aggregation LevelOracle
- Oracle Reporting 1 - Ratio_to_Report FunctionOracleFunction
- Error reporting for dbusError
- GCD-1 Grand Central DispatchGC
- AtCoder Grand Contest 001
- Oracle Appliactions 11i concepts(十五) - Multiple Reporting CurrenciesOracleAPP
- error_reporting()的用法Error
- 讀書筆記【1】 Grand Central Dispatch筆記
- Total Variation(TV)去噪演算法演算法
- Total Video Player for Mac - 超級播霸IDEMac
- 擴充套件部署(Reporting Services 配置)套件
- Improve reporting with duplicate server-idsServer
- 玩轉Total Commander也不難(轉)
- [LeetCode] 2739. Total Distance TraveledLeetCode
- AtCoder Grand Contest 010 C - Cleaning
- Mac超級播霸——Total Video Player for MacMacIDE
- PostgreSQL DBA(62) - PG 12 More progress reportingSQL
- 如何在Total Commander中使用命令列命令列
- Order-level sales credit total () must equal 100%.
- 3186. Maximum Total Damage With Spell CastingAST
- windows 開發者神器 tc – total command和替代品Windows
- XVIII Open Cup named after E.V. Pankratiev. Eastern Grand PrixAST
- oracle10g_asm_v$asm_disk之total_mb_free_mb_required_mirror_free_mb相互關係OracleASMUI
- 程式碼匯出Reporting Services報表檔案
- MRP資訊彙總BAPI(Z_IF_MRP_TOTAL_LIST)API
- Reporting Services 擴充套件裝置資訊設定套件
- XVIII Open Cup named after E.V. Pankratiev. Ukrainian Grand PrixAI
- Total Video Converter Pro全能影片格式轉換工具IDE
- Reporting Services 安裝的備份和還原操作
- DevExpress v17.2新版亮點—.NET Reporting篇(一)devExpress
- DevExpress v17.2新版亮點—.NET Reporting篇(二)devExpress
- [轉]Reporting Services報表服務中的程式設計程式設計