下面是bigquery标准sql 我假设你想知道用户活动当天的平均时间或类似的事情-根据你的陈述 I need to know in which time is where the user do some actions in my app ```
standardSQL
SELECT TIMESTAMP_ADD(ts, INTERVAL avg_value MILLISECOND) FROM ( SELECT TIMESTAMP_TRUNC(ts, DAY) ts, CAST(AVG(TIMESTAMP_DIFF(ts, TIMESTAMP_TRUNC(ts, DAY), MILLISECOND)) AS INT64) avg_value FROM project.dataset.table GROUP BY TIMESTAMP_TRUNC(ts, DAY) )
您可以使用下面的示例中的虚拟数据来测试、播放上面的内容
standardSQL
WITH project.dataset.table AS ( SELECT TIMESTAMP '2020-07-13 00:01:12.616 UTC' ts UNION ALL SELECT '2020-07-13 00:01:14.616 UTC' UNION ALL SELECT '2020-07-13 00:01:15.616 UTC' ) SELECT TIMESTAMP_ADD(ts, INTERVAL avg_value MILLISECOND) FROM ( SELECT TIMESTAMP_TRUNC(ts, DAY) ts, CAST(AVG(TIMESTAMP_DIFF(ts, TIMESTAMP_TRUNC(ts, DAY), MILLISECOND)) AS INT64) avg_value FROM project.dataset.table GROUP BY TIMESTAMP_TRUNC(ts, DAY) )
2条答案
按热度按时间rbpvctlc1#
您可以转换为秒(或毫秒或微秒)并返回到时间戳:
lvmkulzt2#
下面是bigquery标准sql
我假设你想知道用户活动当天的平均时间或类似的事情-根据你的陈述
I need to know in which time is where the user do some actions in my app
```standardSQL
SELECT TIMESTAMP_ADD(ts, INTERVAL avg_value MILLISECOND)
FROM (
SELECT TIMESTAMP_TRUNC(ts, DAY) ts, CAST(AVG(TIMESTAMP_DIFF(ts, TIMESTAMP_TRUNC(ts, DAY), MILLISECOND)) AS INT64) avg_value
FROM
project.dataset.table
GROUP BY TIMESTAMP_TRUNC(ts, DAY)
)
standardSQL
WITH
project.dataset.table
AS (SELECT TIMESTAMP '2020-07-13 00:01:12.616 UTC' ts UNION ALL
SELECT '2020-07-13 00:01:14.616 UTC' UNION ALL
SELECT '2020-07-13 00:01:15.616 UTC'
)
SELECT TIMESTAMP_ADD(ts, INTERVAL avg_value MILLISECOND)
FROM (
SELECT TIMESTAMP_TRUNC(ts, DAY) ts, CAST(AVG(TIMESTAMP_DIFF(ts, TIMESTAMP_TRUNC(ts, DAY), MILLISECOND)) AS INT64) avg_value
FROM
project.dataset.table
GROUP BY TIMESTAMP_TRUNC(ts, DAY)
)
Row f0_
1 2020-07-13 00:01:14.283 UTC