Postgres ROLLUP中如何区分真正的NULL和正式的NULL?
How to distinguish between real NULL and formal NULL in Postgres ROLLUP?
我正在尝试确定每个给定行属于 ROLLUP 报告的哪个“级别”。当初始数据不包含 NULL 时,可以只计算每一行中的空值(对于每个给定的 level/layer 分组,空单元的数量是相同的),尽管在我看来这也不是一个很好的解决方案。
如果初始数据包含 NULL,则此解决方法不再有效:在示例查询结果 table 中,您会看到 <null>
来自真正的空值,而 (null)
是标准分组单元格的 NULL 占位符。
我希望找到类似于分层查询中的“虚拟列”的内容(如 LEVEL
、PATH
等)。
PostgreSQL v12 中是否有一种自然的方法来确定使用 ROLLUP
(通常使用 GROUPING SETS
)构建的报告中行的级别?
ROLLUP查询结果示例:
select a, b, sum(d.c)
from(
select null as a, 2 as b, 1 as c
union all
select null as a, 3 as b, 1 as c
union all
select 'a1' as a, 4 as b, 1 as c
union all
select 'a1' as a, 5 as b, 1 as c
union all
select 'a2' as a, 6 as b, 1 as c
) d GROUP by ROLLUP (d.a, d.b)
| a | b | sum |
--------------------------
| (null) | (null) | 5 |
| a1 | 5 | 1 |
| <null> | 2 | 1 |
| a1 | 4 | 1 |
| a2 | 6 | 1 |
| <null> | 3 | 1 |
| a2 | (null) | 1 |
| <null> | (null) | 2 |
| a1 | (null) | 2 |
--------------------------
使用分组,有点
select case when grouping(a) = 1 then 'Total' else cast(a as varchar(20)) end a,
case when grouping(b) = 1 then 'Total' else cast(b as varchar(20)) end b, sum(d.c)
from(
select null as a, 2 as b, 1 as c
union all
select null as a, 3 as b, 1 as c
union all
select 'a1' as a, 4 as b, 1 as c
union all
select 'a1' as a, 5 as b, 1 as c
union all
select 'a2' as a, 6 as b, 1 as c
) d GROUP by ROLLUP (d.a, d.b)
order by grouping(a), a, grouping(b), b