SQL grouping

SQL grouping solves the problem of totals and subtotals in OLAP scenarios. Its syntax is divided into several categories, but the same problem is to be solved:

ROLLUP and CUBE are GROUPING SETS that encapsulate rules, and GROUPING SETS are the most primitive rules.

For ease of understanding, let's start with a problem and work our way up.

bottom table

The above is the sample bottom table, there are 8 pieces of data, city 1, city 2 two cities, below each have area 1~4, each piece of data has the population number of the data.

Now I want to calculate the total population, as well as the subtotals for each city. Before we master the grouping syntax, we can only get it after unioning two select statements:

SELECT city, sum(people) FROM test GROUP BY city
union
SELECT '合计' as city, sum(people) FROM test
复制代码

But the two select statements are aggregated twice, and the performance is not a small overhead, so SQL provides the GROUPING SETS syntax to solve this problem.

GROUPING SETS

GROUP BY GROUPING SETS can specify any aggregation item. For example, if we want to calculate the total and the grouped total at the same time, we need to perform a GROUP BY sum according to the empty content, and then perform a GROUP BY according to the city, and then perform a sum. The description of GROUPING SETS is:

SELECT 
city, area,
sum(people)
FROM test
GROUP BY GROUPING SETS((), (city, area))
复制代码

where GROUPING SETS((), (city, area))indicates that the total is calculated ()by and (city, area)aggregated respectively. The returned result is:

As you can see, the row with a NULL value is the total we want, and its value is calculated without any GROUP BY restrictions.

Similarly, we can also GROUPING SETS((), (city), (city, area), (area))write any number and combination of GROUP BY conditions.

The data calculated by this rule is called "super group record". We found that NULL values ​​generated by "super grouping records" are easily confused with real NULL values, so SQL provides the GROUPING function to solve this problem.

function GROUPING

The NULL generated by the supergroup record can be recognized as 1 by the GROUPING()function :

SELECT 
GROUPING(city),
GROUPING(area),
sum(people)
FROM test
GROUP BY GROUPING SETS((), (city, area))
复制代码

The specific effect is shown in the following figure:

可以看到,但凡是超级分组计算出来的字段都会识别为 1,我们利用之前学习的 SQL CASE 表达式 将其转换为总计、小计字样,就可以得出一张数据分析表了:

SELECT 
CASE WHEN GROUPING(city) = 1 THEN '总计' ELSE city END,
CASE WHEN GROUPING(area) = 1 THEN '小计' ELSE area END,
sum(people)
FROM test
GROUP BY GROUPING SETS((), (city, area))
复制代码

然后前端表格展示时,将第一行 “总计”、“小计” 单元格合并为 “总计”,就完成了总计这个 BI 可视化分析功能。

ROLLUP

ROLLUP 是卷起的意思,是一种特定规则的 GROUPING SETS,以下两种写法是等价的:

SELECT sum(people) FROM test
GROUP BY ROLLUP(city)

-- 等价于
SELECT sum(people) FROM test
GROUP BY GROUPING SETS((), (city))
复制代码

再看一组等价描述:

SELECT sum(people) FROM test
GROUP BY ROLLUP(city, area)

-- 等价于
SELECT sum(people) FROM test
GROUP BY GROUPING SETS((), (city), (city, area))
复制代码

发现规律了吗?ROLLUP 会按顺序把 GROUP BY 内容 “一个个卷起来”。用 GROUPING 函数判断超级分组记录对 ROLLUP 同样适用。

CUBE

CUBE 又有所不同,它对内容进行了所有可能性展开(所以叫 CUBE)。

类比上面的例子,我们再写两组等价的展开:

SELECT sum(people) FROM test
GROUP BY CUBE(city)

-- 等价于
SELECT sum(people) FROM test
GROUP BY GROUPING SETS((), (city))
复制代码

上面的例子因为只有一项还看不出来,下面两项分组就能看出来了:

SELECT sum(people) FROM test
GROUP BY CUBE(city, area)

-- 等价于
SELECT sum(people) FROM test
GROUP BY GROUPING SETS((), (city), (area), (city, area))
复制代码

所谓 CUBE,是一种多维形状的描述,二维时有 2^1 种展开,三维时有 2^2 种展开,四维、五维依此类推。可以想象,如果用 CUBE 描述了很多组合,复杂度会爆炸。

总结

学习了 GROUPING 语法,以后前端同学的你不会再纠结这个问题了吧:

产品开启了总计、小计,我们是额外取一次数还是放到一起获取啊?

这个问题的标准答案和原理都在这篇文章里了。PS:对于不支持 GROUPING 语法数据库,要想办法屏蔽,就像前端 polyfill 一样,是一种降级方案。至于如何屏蔽,参考文章开头提到的两个 SELECT + UNION。

讨论地址是:精读《SQL grouping》· Issue #406 · ascoders/weekly

如果你想参与讨论,请 点击这里,每周都有新的主题,周末或周一发布。前端精读 - 帮你筛选靠谱的内容。

版权声明:自由转载-非商用-非衍生-保持署名(创意共享 3.0 许可证

Guess you like

Origin juejin.im/post/7083299385416613919