课程: Intermediate SQL for Data Scientists

今天就学习课程吧!

今天就开通帐号,24,700 门业界名师课程任您挑!

Using ROLLUP to create subtotals

Using ROLLUP to create subtotals

- [Instructor] When we work with aggregates, we can use the aggregates, say, to get the totals, maybe the average or some sums or some counts across an entire table. Or we have seen, we can also use the group by expression or the group by clause in the select statement to apply those aggregates to subgroups within, say, a table. So for example, we could maybe group by a department name to see the count of the number of employees within each department. Well, SQL and Postgres actually has another operator we can use called roll up, which gives us a third way of working with aggregates. And roll up allows us to do sort of like sub aggregates in a hierarchical fashion. So for example, in our data model, we have company regions, and these regions are part of countries. So we could, for example, with roll up, get our total number of employees by region and then also get a total count by country and then also a total count by the whole table of employees. So that's what the roll up operator…

内容