0

可能是一个初学者的问题,但我在这里找不到灵魂。

我想从我的谷歌分析帐户中导出 BigQuery 中的不同事件(仅总事件指标)

因此,我有一个下载事件和一个搜索事件,我希望将每个事件放在一个列中,向我显示每个事件的总事件指标。我针对不同的条件过滤了这两个事件,以确保我有正确的事件。

我在这里使用 LegacySQL 并希望从同一个表中导出所有数据。问题是,LegacySQL 中不允许子选择,有什么想法可以解决这个问题吗?

SELECT  clientId, totals.timeOnSite, trafficSource.source,
(SELECT hits.eventInfo.eventAction FROM (TABLE_DATE_RANGE([xxxxxx.ga_sessions_], TIMESTAMP('2018-11-25'), TIMESTAMP('2018-11-25')))   WHERE hits.eventInfo.eventCategory = 'Download' AND hits.type = 'EVENT' AND hits.eventInfo.eventAction = 'pdf') AS DownloadEvent,
(SELECT hits.eventInfo.eventLabel FROM (TABLE_DATE_RANGE([xxxxxx.ga_sessions_], TIMESTAMP('2018-11-25'), TIMESTAMP('2018-11-25')))   WHERE hits.eventInfo.eventLabel  = 'search-header' OR 'search-mainpage' AND hits.type = 'EVENT') AS SearchEvent,

COUNT(1) eventHits FROM (TABLE_DATE_RANGE([xxxxxx.ga_sessions_], TIMESTAMP('2018-11-25'), TIMESTAMP('2018-11-25')))
4

1 回答 1

0

BQ Legacy SQL 中允许子选择。下面应该给你你正在寻找的结果。

您不需要在这里使用子查询。使用一个简单的CASE语句应该可以解决问题。case 语句将过滤与您正在查找的条件匹配的行。

询问

SELECT 
clientId,
totals.timeOnSite, 
trafficSource.source,
SUM(COUNT(CASE WHEN (hits.eventInfo.eventCategory = 'Download' AND hits.type = 'EVENT' AND hits.eventInfo.eventAction = 'pdf') THEN hits.eventInfo.eventAction END)) AS DownloadEvent,
SUM(COUNT(CASE WHEN (hits.eventInfo.eventLabel  = 'search-header' OR hits.eventInfo.eventLabel  = 'search-mainpage') AND hits.type = 'EVENT' THEN hits.eventInfo.eventLabel END)) AS SearchEvent
FROM TABLE_DATE_RANGE([xxxxxx.ga_sessions_], TIMESTAMP('2018-11-25'), TIMESTAMP('2018-11-25'))
GROUP BY 1,2,3

现在,如果您想使用子查询,它的外观如下

带子查询

SELECT  
b.clientId, 
b.totals.timeOnSite, 
b.trafficSource.source,
SUM(a.DownloadEvent),
SUM(a.SearchEvent)
FROM
(TABLE_DATE_RANGE([xxxxxx.ga_sessions_], TIMESTAMP('2018-11-25'), TIMESTAMP('2018-11-25'))) AS b 
INNER JOIN (
SELECT 
clientId,
totals.timeOnSite, 
trafficSource.source,
COUNT(CASE WHEN (hits.eventInfo.eventCategory = 'Download' AND hits.type = 'EVENT' AND hits.eventInfo.eventAction = 'pdf') THEN hits.eventInfo.eventAction END) AS DownloadEvent,
COUNT(CASE WHEN (hits.eventInfo.eventLabel  = 'search-header' OR hits.eventInfo.eventLabel  = 'search-mainpage') AND hits.type = 'EVENT' THEN hits.eventInfo.eventLabel END) AS SearchEvent
FROM TABLE_DATE_RANGE([xxxxxx.ga_sessions_], TIMESTAMP('2018-11-25'), TIMESTAMP('2018-11-25'))
GROUP BY 1
) AS a ON b.clientId = a.clientId
GROUP BY 1,2,3
于 2018-12-03T16:42:47.320 回答