我想获取当前行和上一行之间的秒数差异,并在订单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
答案 0 :(得分:3)
请尝试以下
我建议你查看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