sql - oracle 中报告表的数据馈送(汇总与分组集)

标签 sql oracle rollup

我有一个问题:

select country_region, 
       country_subregion, 
       country_name, 
       calendar_year, 
       calendar_quarter_number, 
       sum(amount_sold) as amount
  from countries co join
       customers cu on co.country_id = cu.country_id join
       sales sa on cu.cust_id = sa.cust_id join
       times ti on sa.time_id = ti.time_id
 where (   co.country_region = 'Americas' 
        or co.country_region = 'Middle East'
       ) 
   and ti.calendar_year between 2000 and 2001
group by grouping sets 
(
    (country_region, country_subregion, country_name, calendar_year, calendar_quarter_number),
    (country_region, country_subregion, country_name, calendar_year),
    (country_region, country_subregion, country_name),
    (country_region, country_subregion, calendar_year, calendar_quarter_number),
    (country_region, country_subregion, calendar_year),
    (country_region, country_subregion),
    (country_region, calendar_year, calendar_quarter_number),
    (country_region, calendar_year),
    (country_region),
    (calendar_year, calendar_quarter_number),
    (calendar_year),
    ()
)
order by amount desc;

返回相同输出但使用group by rollup 子句的查询是什么? 我想要一个查询。

最佳答案

使用 ROLLUP 子句的等效查询是这样的:

select country_region
     , country_subregion
     , country_name
     , calendar_year
     , calendar_quarter_number
     , sum(amount_sold) as amount
  from countries co
       join customers cu on co.country_id = cu.country_id
       join sales sa on cu.cust_id = sa.cust_id
       join times ti on sa.time_id = ti.time_id
 where (  co.country_region='Americas'
       or co.country_region='Middle East'
       )
   and ti.calendar_year between 2000 and 2001
 group by rollup (country_region, country_subregion, country_name)
     , rollup (calendar_year, calendar_quarter_number)
 order by amount desc

证明如下:

 group by rollup (country_region, country_subregion, country_name)
     , rollup (calendar_year, calendar_quarter_number)

等于

 group by grouping sets
       ( (country_region, country_subregion, country_name)
       , (country_region, country_subregion)
       , (country_region)
       , ()
       )
     , grouping sets
       ( (calendar_year, calendar_quarter_number)
       , (calendar_year)
       , ()
       )

等于

 group by grouping sets
       ( (country_region, country_subregion, country_name, calendar_year, calendar_quarter_number)
       , (country_region, country_subregion, country_name, calendar_year)
       , (country_region, country_subregion, country_name)
       , (country_region, country_subregion, calendar_year, calendar_quarter_number)
       , (country_region, country_subregion, calendar_year)
       , (country_region, country_subregion)
       , (country_region, calendar_year, calendar_quarter_number)
       , (country_region, calendar_year)
       , (country_region)
       , (calendar_year, calendar_quarter_number)
       , (calendar_year)
       , ()
       )

这等于您的原始查询。

您可以在我去年写的这篇文章中找到有关按扩展分组的更多信息:http://www.rwijk.nl/AboutOracle/All_About_Grouping.pdf

问候, 罗布。

关于sql - oracle 中报告表的数据馈送(汇总与分组集),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4566989/

相关文章:

mysql - MySQL where 子句中子查询返回多于 1 行

mysql - 我应该尽可能避免复合主键吗?

sql - 分组集、汇总但具有多列

mysql - 在 ROLLUP 中用 SUBTOTAL 和 TOTAL 替换 NULL

sql - 混合显式和隐式连接

SQL Server - 来自不同表的 CTE 递归 SUM 值

Oracle - 最佳句子 : IN(), like、REGEXP_LIKE、otherone

sql - 将行对与交换的列组合起来

java - Oracle 11g - 如何调用内部带有 DML 的函数?

d3.js - 使用 Visual Studio Code 汇总 @types/d3