计算同一位访客的第一次交易日期和第一次会话日期之间的小时差。
嗨!
我需要计算同一位访客的第一笔交易和第一次会话的日期之间的小时数差异。
现在我有一张桌子,只有来访者有购物,并且我已经有了Time_Start_session,但是我不知道如何获取第一次交易的时间...
我一直在想,首先在transactionUserCount = 1中创建一个过滤器,但是这给我一个名称无法识别的错误。而且,使用字段日期来获取这些匹配的日期和小时,这些匹配仅包含第一笔交易。但是还有另一个问题,我不知道如何将字段日期转换为Timestamp的时间戳。
顺便说一句,transactionUserCount为我们提供了每个fullvisitorId的购买数量。
我的重点是在这两列之间休息一下,但是我没有得到结果...
请介意帮我一下吗?
或者,如果您知道一种更好的方法,我将非常感谢您。
最好
#standardSQL
SELECT
fullVisitorId,
hit.transaction.transactionId,
hit.transaction.transactionRevenue / 1000000 AS tRevenue,
visitNumber,
hit.hitNumber,
LAG(visitNumber) OVER(PARTITION BY fullVisitorId ORDER BY visitNumber ASC, hit.hitNumber ASC ) AS prev_session,
IF( LAG(visitNumber) OVER(PARTITION BY fullVisitorId ORDER BY visitNumber ASC, hit.hitNumber ASC ) IS NULL,
visitNumber,
visitNumber - LAG(visitNumber) OVER(PARTITION BY fullVisitorId ORDER BY visitNumber ASC, hit.hitNumber ASC )) AS session_diff,
ROW_NUMBER() OVER(PARTITION BY fullVisitorId ORDER BY visitNumber ASC, hit.hitNumber ASC ) AS transactionUserCount,
TIMESTAMP_SECONDS(visitStartTime) AS Time_Start_Session,
PARSE_TIMESTAMP('%s', date) AS Time_Transaction,
IF( ROW_NUMBER() OVER(PARTITION BY fullVisitorId ORDER BY visitNumber ASC, hit.hitNumber ASC ) =1,
0,
(visitStartTime - LAG(visitStartTime) OVER(PARTITION BY fullVisitorId ORDER BY visitNumber ASC, hit.hitNumber ASC))/3600) AS Hours_Between_Transaction
FROM
`ads-data-hub-182820.tradeinn_190131.ga_sessions_*`,
UNNEST(hits) AS hit
WHERE
(_TABLE_SUFFIX BETWEEN '20180101'
AND '20191231')
AND hit.transaction.transactionId IS NOT NULL
AND hit.transaction.transactionRevenue IS NOT NULL
ORDER BY
fullVisitorID ASC,
visitNumber ASC,
hit.hitNumber ASC
Time_Start_Session像这样:2018-11-09 20:56:49 UTC,而Time_Transaction像这样:1970-08-22 13:51:49 UTC。 显然,当我尝试在时间戳中转换日期时,某些操作不起作用