我正在一个项目,该项目从BigQuery中的Google Analytics(分析)数据构建一些查询,以为某个特定的KPI复制一些报告,我有一个表格,其中列出了一些网站,这些网站需要从Google Analytics(分析)数据中排除以获得正确的指标。
我的列表可能包含以下内容:
sitename.com
但是我需要将其与BigQuery中的eventLabel列匹配,URL可能会返回为:
http://sitename.com/subpage/extra-subpage
我无法进行Not In,因为这需要直接匹配,我尝试使用like语句,但是出现以下错误
Scalar subquery produced more than one element
我不太确定该如何继续进行操作,并且想知道是否需要执行一个查询,该查询说字符串是否匹配(因为如果我使用内部联接然后使用此新表可以使它工作,可以排除,因为我可以保留eventLabel,然后根据该标签进行“不在”?
SELECT Distinct
h.eventinfo.eventAction eventAction,
h.eventinfo.eventlabel eventlabel
FROM `projectName.ga_sessions_*`, unnest(Hits) h
WHere
_TABLE_SUFFIX BETWEEN "20190101" AND FORMAT_DATE('%Y%m%d',DATE_SUB(CURRENT_DATE(), INTERVAL 1 DAY))
and type = 'EVENT'
and h.eventInfo.eventCategory = 'EventName'
and Replace(Replace(Replace(h.eventInfo.eventLabel,'http://',''),'https://',''),'www.','')
Not like (select concat(ThirdPartyURL,'%') from `projectName.datasetName.ExclusionList`)
我希望以上所述是合理的。
TIA。
答案 0 :(得分:1)
重现问题后,解决方案是使用NOT IN
代替NOT LIKE
,如下所示:
WITH `projectName.datasetName.ExclusionList` AS
(SELECT 'label1' AS ThirdPartyURL UNION ALL
SELECT 'label2')
SELECT DISTINCT h.eventinfo.eventAction eventAction,
h.eventinfo.eventlabel eventlabel
FROM `bigquery-public-data.google_analytics_sample.ga_sessions_*`,
unnest(Hits) h
WHERE _TABLE_SUFFIX BETWEEN "20170801" AND "20170802"
AND TYPE = 'EVENT'
AND h.eventInfo.eventCategory = 'EventName'
AND Replace(Replace(Replace(h.eventInfo.eventLabel, 'http://', ''), 'https://', ''), 'www.', '')
NOT IN
(SELECT ThirdPartyURL FROM `projectName.datasetName.ExclusionList`)
这是link与BigQuery相关的SQL文档