当前行和上一行之间的秒数差异,并在订单 ID 也使用 google bigquery 匹配时将值存储在单独的列中
Posted
技术标签:
【中文标题】当前行和上一行之间的秒数差异,并在订单 ID 也使用 google bigquery 匹配时将值存储在单独的列中【英文标题】:Difference in seconds between current row and previous row and store the value in separate column when order ID also matches using google bigquery 【发布时间】:2016-05-31 22:04:04 【问题描述】:当订单 ID 也使用 google bigquery 匹配时,我想获取当前行和上一行之间的秒数差异并将值存储在单独的列中。 PS:第 4 行根据第 7 行取差值,因为这是下一个可用的订单 ID (日期字段不是字符串而是时间戳)在我的表 test:product.tab1 例如:
Date Order Difference In Seconds
2016-05-31 11:46:54 UTC 14567 11
2016-05-31 11:46:43 UTC 14567 19
2016-05-31 11:46:24 UTC 14567 20
2016-05-31 11:46:04 UTC 14567 4
2016-05-31 11:46:54 UTC 22455 11
2016-05-31 11:46:43 UTC 24567 0
2016-05-31 11:46:00 UTC 14567 0
【问题讨论】:
【参考方案1】:试试下面 我建议您查看Window functions
SELECT
DATE, id,
IFNULL(TIMESTAMP_TO_SEC(TIMESTAMP(DATE)) -
TIMESTAMP_TO_SEC(TIMESTAMP(prev_date))
, 0) AS Difference_In_Seconds
FROM (
SELECT
DATE, id,
LEAD(DATE) OVER(PARTITION BY id ORDER BY DATE DESC) AS prev_date
FROM
(SELECT STRING(DATE) AS DATE, id FROM [test:product.tab1] )
)
ORDER BY id, DATE DESC
【讨论】:
我试图优化这样的查询 SELECT DATE, id, date - LEAD(DATE) OVER(PARTITION BY id ORDER BY DATE DESC) AS diff FROM (SELECT STRING(DATE) AS date, id FROM [ test:product.tab1] order by id date desc) 但我收到一个错误 - 分析表达式中缺少函数,你知道我为什么不能这样做吗?以上是关于当前行和上一行之间的秒数差异,并在订单 ID 也使用 google bigquery 匹配时将值存储在单独的列中的主要内容,如果未能解决你的问题,请参考以下文章