OLAP 操作:GROUPING SETS

用 SQL 进行数据驱动决策

Irene Ortner

Data Scientist at Applied Statistics

SQL 中的 OLAP 运算符概览

用于支持 OLAP 操作的 SQL 扩展

  • GROUP BY CUBE
  • GROUP BY ROLLUP
  • GROUP BY GROUPING SETS
用 SQL 进行数据驱动决策

表 renting_extended

renting_extended 表的前几行:

| renting_id | country  | genre  | rating |
|------------|----------|--------|--------|
| 2          | Belgium  | Drama  | 10     |
| 32         | Belgium  | Drama  | 10     |
| 203        | Austria  | Drama  | 6      |
| 292        | Austria  | Comedy | 8      |
| 363        | Belgium  | Drama  | 7      |
| .......... | ........ | ...... | ...... |
用 SQL 进行数据驱动决策

GROUP BY GROUPING SETS

使用 GROUPING SETS 的查询示例:

SELECT country, 
       genre, 
       COUNT(*)
FROM rentings_extended
GROUP BY GROUPING SETS ((country, genre), (country), (genre), ());
  • 用圆括号包裹的列名表示一层聚合。
  • GROUP BY GROUPING SETS 返回多个 GROUP BY 查询的 UNION
用 SQL 进行数据驱动决策

GROUPING SETS 与 GROUP BY 查询

SELECT country, 
       genre, 
       COUNT(*)
FROM renting_extended
GROUP BY GROUPING SETS (country, genre);
  • 按国家与类型的唯一组合统计租赁数。
  • GROUPING SETS 中的表达式:(country, genre)
SELECT country, 
       genre, 
       COUNT(*)
FROM renting_extended
GROUP BY country, genre;
| country | genre  | count  |
|---------|--------|--------|
| Austria | Comedy | 2      |
| Belgium | Drama  | 15     |
| Austria | Drama  | 4      |
| Belgium | Comedy | 1      |
用 SQL 进行数据驱动决策

GROUPING SETS 与 GROUP BY 查询

SELECT country, COUNT(*)
FROM renting_extended
GROUP BY GROUPING SETS (country);
  • 按国家统计电影租赁数。
  • GROUPING SETS 中的表达式:(country)
SELECT country, COUNT(*)
FROM renting_extended
GROUP BY country;
| country | count |
|---------|-------|
| Austria | 16    |
| Belgium | 6     |
用 SQL 进行数据驱动决策

GROUPING SETS 与 GROUP BY 查询

SELECT genre, COUNT(*)
FROM renting_extended
GROUP BY GROUPING SETS (genre);
  • 按类型统计电影租赁数。
  • GROUPING SETS 中的表达式:(genre)
SELECT genre, COUNT(*)
FROM renting_extended
GROUP BY genre;
| country | count |
|---------|-------|
| Comedy  | 3     |
| Drama   | 19    |
用 SQL 进行数据驱动决策

GROUPING SETS 与 GROUP BY 查询

SELECT COUNT(*)
FROM renting_extended
GROUP BY GROUPING SETS ();
  • 总聚合:统计全部电影租赁数。
  • GROUPING SETS 中的表达式:()
SELECT COUNT(*)
FROM renting_extended;
| count |
|-------|
| 22    |
用 SQL 进行数据驱动决策

GROUP BY GROUPING SETS 记法

  • GROUP BY GROUPING SETS (...)
    SELECT country, genre, COUNT(*)
    FROM renting_extended
    GROUP BY GROUPING SETS ((country, genre), (country), (genre), ());
    
  • 对前 4 个查询做 UNION
  • 在一条查询中汇总数据透视表的全部信息。
  • 等价于 GROUP BY CUBE (country, genre)
用 SQL 进行数据驱动决策

使用 GROUPING SETS 的结果

SELECT country, genre, COUNT(*)
FROM renting_extended
GROUP BY GROUPING SETS ((country, genre), (country), (genre), ());
| country | genre  |count|
|---------|--------|-----|
| NULL    | NULL   | 22  |
| Austria | Comedy | 2   |
| Belgium | Drama  | 15  |
| Austria | Drama  | 4   |
| Belgium | Comedy | 1   |
| Belgium | NULL   | 16  |
| Austria | NULL   | 6   |
| NULL    | Comedy | 3   |
| NULL    | Drama  | 19  |
用 SQL 进行数据驱动决策

计算租赁次数与平均评分

  • 仅组合选定的聚合:
    • country 与 genre
    • genre
  • 使用租赁次数与平均评分聚合。
    SELECT country, 
         genre, 
         COUNT(*), 
         AVG(rating) AS avg_rating
    FROM renting_extended
    GROUP BY GROUPING SETS ((country, genre), (genre));
    
用 SQL 进行数据驱动决策

计算租赁次数与平均评分

SELECT country, genre, COUNT(*), AVG(rating) AS avg_rating
FROM renting_extended
GROUP BY GROUPING SETS ((country, genre), (genre));
| country | genre  | count| avg_rating |
|---------|--------|------|------------|
| Austria | Comedy | 2    | 8.00       |
| Belgium | Drama  | 15   | 9.17       |
| Austria | Drama  | 4    | 6.00       |
| Belgium | Comedy | 1    | NULL       |
| NULL    | Comedy | 3    | 8.00       |
| NULL    | Drama  | 19   | 8.38       |
用 SQL 进行数据驱动决策

Passons à la pratique !

用 SQL 进行数据驱动决策

Preparing Video For Download...