BigQuery:识别交易前的所有命中
Posted
技术标签:
【中文标题】BigQuery:识别交易前的所有命中【英文标题】:BigQuery: identify all hits preceding a transaction 【发布时间】:2019-02-11 14:05:33 【问题描述】:假设以下 BQ 查询:
SELECT * FROM (
SELECT CONCAT(CAST(fullVisitorId AS String), CAST(visitId AS STRING)) AS CustVisitId, hits.hitnumber AS HitNr, hits.transaction.transactionRevenue/1000000 AS Rev
FROM
`[projectid].[dataset].ga_sessions_*` AS t, t.hits AS hits
WHERE
_TABLE_SUFFIX BETWEEN '20190120' AND '20190120'
ORDER BY HitNr)
WHERE CustVisitId = '49889154628941922861547956449'
这会产生一个像这样的表格:
CustVisitId HitNr Rev
123456789 1
123456789 2
123456789 3
123456789 4 8
123456789 5
123456789 6
123456789 7
123456789 8
123456789 9 40
123456789 10
现在,我想添加一列(以下示例中的“新”)来识别交易之前(包括)交易之前的所有点击,如下所示:
CustVisitId HitNr Rev New
123456789 1 1
123456789 2 1
123456789 3 1
123456789 4 8 1
123456789 5 2
123456789 6 2
123456789 7 2
123456789 8 2
123456789 9 40 2
123456789 10 3
有谁知道如何调整查询以生成所需的表?
【问题讨论】:
【参考方案1】:以下是 BigQuery 标准 SQL
#standardSQL
SELECT *,
1 + (COUNT(Rev) OVER(PARTITION BY CustVisitId ORDER BY HitNr ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING)) `New`
FROM (
SELECT CONCAT(CAST(fullVisitorId AS STRING), CAST(visitId AS STRING)) AS CustVisitId, hits.hitnumber AS HitNr, hits.transaction.transactionRevenue/1000000 AS Rev
FROM
`[projectid].[dataset].ga_sessions_*` AS t, t.hits AS hits
WHERE
_TABLE_SUFFIX BETWEEN '20190120' AND '20190120'
ORDER BY HitNr)
WHERE CustVisitId = '49889154628941922861547956449'
如您所见,我没有更改您的查询,只是为 New
添加了一行
【讨论】:
不错!很高兴它有帮助。也考虑投票:o) 刚刚做了米哈伊尔:)以上是关于BigQuery:识别交易前的所有命中的主要内容,如果未能解决你的问题,请参考以下文章