简体   繁体   English

Oracle SQL中四列上的ROLLUP小计级别

[英]Subtotal levels for ROLLUP on four columns in Oracle SQL

I'm trying to construct an Oracle SQL query using the ROLLUP operator. 我正在尝试使用ROLLUP运算符构造一个Oracle SQL查询。 The query needs to summarize sales by year, quarter, store state, and store city over two years, with the result including subtotals for the two hierarchical dimensions of year/quarter and state/city. 该查询需要汇总两年中的年,季度,商店状态和商店城市的销售额,其结果包括年/季度和州/城市两个层次维度的小计。 Here's my attempt thus far: 到目前为止,这是我的尝试:

SELECT storestate, storecity, calyear, calquarter, SUM(sales) AS Sales
FROM store_dim, time_dim, sales_fact
WHERE sales_fact.storeid = store_dim.storeid
    AND sales_fact.timenum = time_dim.timenum
    AND (calyear BETWEEN 2011 AND 2012)
GROUP BY ROLLUP(calyear, calquarter, storestate, storecity);

I'm trying to figure out if, as it's currently written, the query is showing subtotals for the two hierarchies I'm looking for, rather than treating them as one big one. 我试图确定查询是否显示了我要查找的两个层次结构的小计,而不是将它们视为一个大的层次结构。 Attempting to map out the subtotal levels by hand didn't help, and I haven't been able to find any examples of a single ROLLUP with four columns from two dimensions, or an example of two ROLLUP operators in a single GROUP BY clause, like below: 尝试手动绘制小计级别无济于事,而且我也找不到任何从两个维度包含四列的单个ROLLUP的示例,或者在单个GROUP BY子句中找到两个ROLLUP运算符的示例,如下所示:

GROUP BY ROLLUP(calyear, cal quarter), ROLLUP(storestate, storecity)

A breakdown of the subtotal levels produced by the two GROUP BY clauses would be hugely helpful. 对两个GROUP BY子句产生的小计级别进行细分将非常有帮助。

Edit: I'm specifically to use ROLLUP here. 编辑:我专门在这里使用ROLLUP。 GROUPING SETS would generally be the first choice for this kind of query otherwise. 否则,分组集通常是此类查询的首选。

Use grouping sets . 使用grouping sets . . and proper, explicit, standard join syntax: 以及正确,明确, 标准的 join语法:

select s.storestate, s.storecity, t.calyear, t.calquarter,
       sum(sf.sales) AS Sales
from sales_fact sf join
     store_dim s
     on s.storeid = sf.storeid join
     time_dim t 
     on sf.timenum = t.timenum
where calyear between 2011 and 2012
group by grouping sets ( (calyear, calquarter, storestate, storecity),
                         (calyear, calquarter), (storestate, storecity)
                       );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM