I'm having a problem with count distinct + case when / with group by rollup in BigQuery. The result did not as I expected. Here's the code:
with tb1 as
(
select 'DN' as geography
,'1012658993824' as SKU
,1 as pageview
union all
select 'KR' as geography
,'1012658993824' as SKU
,7 as pageview
)
select geography
,count(distinct(case when pageview between 1 and 5 then SKU end)) as PV_from_0_to_5
,count(distinct(case when pageview between 6 and 10 then SKU end)) as PV_from_6_to_10
from tb1
group by rollup (1)
**Output:**
geography/ PV_from_0_to_5/ PV_from_6_to_10
NULL 1 1
DN 1 0
KR 0 1
**Expected Output**
geography/ PV_from_0_to_5/ PV_from_6_to_10
NULL 0 1
DN 1 0
KR 0 1
Explain: Obviously, I'm counting pageview of a single SKU across two location: DN & KR. The problem is when using group by rollup, I expect the pageview would be aggregated (1+7 = 8 pageviews) before count distinct the SKU. I cannot put a SUM within a count distinct, hence I don't know what else can do.
