这只是一个好奇心:当我使用group by时,为什么配置单元不显示空分区计数?
select count(*), partition_date
from table
group by partition_date;
结果是:
count----------------partition_date
746 ----------------20160901
1155----------------20160906
589 ----------------20160907
639 ----------------20160908
763 ----------------20160909
1502----------------20160912
1188----------------20160913
601 ----------------20160914
675 ----------------20160915
766 ----------------20160916
现在我无法看到3个分区:20160905,20160904,20160903。
如果我对这些特定分区进行计数,它会给我count = 0。
select count(*) from table where partition_date=20160905;
select count(*) from table where partition_date=20160904;
select count(*) from table where partition_date=20160903;
但是这样我没有使用group by就做到了,这就是为什么它给了我一个结果。
如果我再次使用group by,我仍然会得到无效结果
select count(*), partition_date from table where partition_date=20160905 group by partition_date;
我尝试过其他方式,但我仍然无法使用group by ...来获取空分区的数量。
select count(*), partition_date from table group by partition_date having count(*)>=0;
select count(*), partition_date from table group by partition_date having count(*)=0;
select count(*), partition_date from table where partition_date in (20160905, 20160904, 20160903) group by partition_date;
这是我找到的唯一解决方案,还有另一种方法吗?
select AA.partition_date,nvl(BB.CN,0) as CC
from (select distinct partition_date as partition_date from table) AA
left join
(select partition_date, count(*) as CN from table group by partition_date) BB
on AA.partition_date = BB.partition_date;
所以我最后的问题是: