我想计算包含一些客户标记的已查看页面的数量,我的代码目前返回所有3个计数的相同值,这些值太高,有人可以建议问题在这里吗?
SELECT
Date,
COUNT(CASE WHEN hits.page.pagepath LIKE "?aff=" THEN 1 ELSE 0 END) AS Affinity,
COUNT(CASE WHEN hits.page.pagepath LIKE "?aff2=" THEN 1 ELSE 0 END) AS Addon_Affinity,
COUNT(CASE WHEN hits.page.pagepath LIKE "?aff3=" THEN 1 ELSE 0 END) AS Style_Affinity
FROM `[tablename].ga_sessions_*` AS t
CROSS JOIN UNNEST(hits) AS hits
WHERE (_TABLE_SUFFIX BETWEEN '20170703' AND '20170703')
group by date
答案 0 :(得分:1)
如果输入不为空,COUNT会将其总和增加1。您应该使用SUM或COUNTIF:
SELECT
Date,
COUNTIF(hits.page.pagepath LIKE "?aff=") AS Affinity,
COUNTIF(hits.page.pagepath LIKE "?aff2=") AS Addon_Affinity,
COUNTIF(hits.page.pagepath LIKE "?aff3=") AS Style_Affinity
FROM `[tablename].ga_sessions_*` AS t
CROSS JOIN UNNEST(hits) AS hits
WHERE (_TABLE_SUFFIX BETWEEN '20170703' AND '20170703')
group by date
答案 1 :(得分:1)
其他可能性是解决此查询,如:
common
其中WITH data AS(
select "20170701" as date, ARRAY<STRUCT<page STRUCT<pagePath STRING> >> [STRUCT(STRUCT('?aff=' as pagePath) as page), STRUCT(STRUCT('?aff=' as pagePath) as page), STRUCT(STRUCT('/test' as pagePath) as page), STRUCT(STRUCT('?aff2=' as pagePath) as page), STRUCT(STRUCT('?aff=' as pagePath) as page)] hits union all
select "20170701" as date, ARRAY<STRUCT<page STRUCT<pagePath STRING> >> [STRUCT(STRUCT('?aff2=' as pagePath) as page), STRUCT(STRUCT('?aff2=' as pagePath) as page), STRUCT(STRUCT('?aff3=' as pagePath) as page), STRUCT(STRUCT('?aff3=' as pagePath) as page), STRUCT(STRUCT('?aff=' as pagePath) as page)] hits
)
SELECT
date,
sum((select countif(hits.page.pagepath like "?aff=") from unnest(hits) hits)) Affinity,
sum((select countif(hits.page.pagepath like "?aff2=") from unnest(hits) hits)) Addon_Affinity,
sum((select countif(hits.page.pagepath like "?aff3=") from unnest(hits) hits)) Style_Affinity
FROM data
GROUP BY
date
是您data
数据的模拟。