BigQuery,SQL:从两个时间戳中提取时间差
我有一张这样的桌子:BigQuery,SQL:从两个时间戳中提取时间差,sql,datetime,google-bigquery,Sql,Datetime,Google Bigquery,我有一张这样的桌子: event_timestamp SESSION_ID 2021-01-24 15:22:33.761011 UTC 0046aa0e-5035-4b2d-a6de-77a47949cdf6 2021-01-24 15:22:33.761011 UTC 0046aa0e-5035-4b2d-a6de-77a47949cdf6 2021-01-24 15:22:53.335013 UTC
event_timestamp SESSION_ID
2021-01-24 15:22:33.761011 UTC 0046aa0e-5035-4b2d-a6de-77a47949cdf6
2021-01-24 15:22:33.761011 UTC 0046aa0e-5035-4b2d-a6de-77a47949cdf6
2021-01-24 15:22:53.335013 UTC 0046aa0e-5035-4b2d-a6de-77a47949cdf6
2021-01-24 15:24:53.700444 UTC 0046aa0e-5035-4b2d-a6de-77a47949cdf6
2021-01-24 15:25:05.362517 UTC 0046aa0e-5035-4b2d-a6de-77a47949cdf6
2021-01-24 15:25:05.362517 UTC 0046aa0e-5035-4b2d-a6de-77a47949cdf6
2021-01-24 15:25:48.949679 UTC 0046aa0e-5035-4b2d-a6de-77a47949cdf6
2021-01-24 15:25:48.949679 UTC 0046aa0e-5035-4b2d-a6de-77a47949cdf6
2021-01-24 15:27:47.543331 UTC 0046aa0e-5035-4b2d-a6de-77a47949cdf6
2021-01-24 15:27:47.543331 UTC 0046aa0e-5035-4b2d-a6de-77a47949cdf6
2021-01-24 15:28:22.442896 UTC 0046aa0e-5035-4b2d-a6de-77a47949cdf6
2021-01-24 15:28:22.442896 UTC 0046aa0e-5035-4b2d-a6de-77a47949cdf6
2021-01-24 15:34:41.572305 UTC 0046aa0e-5035-4b2d-a6de-77a47949cdf6
2021-01-24 16:37:28.988887 UTC 0046aa0e-5035-4b2d-a6de-77a47949cdf6
2021-01-24 16:37:28.988887 UTC 0046aa0e-5035-4b2d-a6de-77a47949cdf6
我需要计算平均会话时间:
with cte as (
SELECT
timestamp_micros(event_timestamp) as timestamp,
min(timestamp_micros(event_timestamp)) over (partition by SESSION_ID) as min_time,
max(timestamp_micros(event_timestamp)) over (partition by SESSION_ID) as max_time,
SESSION_ID
FROM 'table'
)
select
timestamp
round(avg (timestamp_diff(min_time,max_time,second)) over (partition by c.SESSION_ID)/60,2) as AVG_session_time,
SESSION_ID
输出:
event_timestamp AVG_session_time SESSION_ID
2021-01-24 15:22:33.761011 UTC 74.92 0046aa0e-5035-4b2d-a6de-77a47949cdf6
2021-01-24 15:22:33.761011 UTC 74.92 0046aa0e-5035-4b2d-a6de-77a47949cdf6
2021-01-24 15:22:53.335013 UTC 74.92 0046aa0e-5035-4b2d-a6de-77a47949cdf6
2021-01-24 15:24:53.700444 UTC 74.92 0046aa0e-5035-4b2d-a6de-77a47949cdf6
2021-01-24 15:25:05.362517 UTC 74.92 0046aa0e-5035-4b2d-a6de-77a47949cdf6
2021-01-24 15:25:05.362517 UTC 74.92 0046aa0e-5035-4b2d-a6de-77a47949cdf6
2021-01-24 15:25:48.949679 UTC 74.92 0046aa0e-5035-4b2d-a6de-77a47949cdf6
2021-01-24 15:25:48.949679 UTC 74.92 0046aa0e-5035-4b2d-a6de-77a47949cdf6
2021-01-24 15:27:47.543331 UTC 74.92 0046aa0e-5035-4b2d-a6de-77a47949cdf6
2021-01-24 15:27:47.543331 UTC 74.92 0046aa0e-5035-4b2d-a6de-77a47949cdf6
2021-01-24 15:28:22.442896 UTC 74.92 0046aa0e-5035-4b2d-a6de-77a47949cdf6
2021-01-24 15:28:22.442896 UTC 74.92 0046aa0e-5035-4b2d-a6de-77a47949cdf6
2021-01-24 15:34:41.572305 UTC 74.92 0046aa0e-5035-4b2d-a6de-77a47949cdf6
2021-01-24 16:37:28.988887 UTC 74.92 0046aa0e-5035-4b2d-a6de-77a47949cdf6
2021-01-24 16:37:28.988887 UTC 74.92 0046aa0e-5035-4b2d-a6de-77a47949cdf6
但74.92不是时差,因为这是74分和60秒的92%
所以我需要得到一个0.92*60/100=74:55的答案
我无法提取秒数百分比并计算秒数,因为文本和数字之间的转换和提取存在问题。。。。。。
是否有其他方法以时间(分和秒)的形式获得结果
谢谢)请考虑以下内容
select average_session_duration,
format(
'%i day %i hour %i minute %i second',
unix_date(date(timestamp_seconds(average_session_duration))),
extract(hour from time(timestamp_seconds(average_session_duration))),
extract(minute from time(timestamp_seconds(average_session_duration))),
extract(second from time(timestamp_seconds(average_session_duration)))
) as average_duration_as_long_string,
regexp_replace(
cast(time(timestamp_seconds(average_session_duration)) as string),
r'^\d\d',
cast(extract(hour from time(timestamp_seconds(average_session_duration))) + 24 * unix_date(date(timestamp_seconds(average_session_duration))) as string)
) as average_duration_as_short_string
from (
select cast(avg(session_duration) as int64) as average_session_duration from (
select
timestamp_diff(max(event_timestamp), min(event_timestamp), second) session_duration
from `project.dataset.table`
group by SESSION_ID
)
)
如果要应用于问题中的样本数据,则输出为
你的平均成绩不是分钟,而是秒。74.92不是“74分92%的60秒”,而是74秒92%的1秒或920毫秒。这是1分14.92秒。不是你要求的答案,但我希望它能有所帮助。但是如果一个函数以秒为单位:timestamp_diff(min_time,max_time,second),它不会以秒为单位返回结果吗?您好,感谢您在解决方案中的回复,获得的结果是所有会话的结果。例如,如果我有很多课程,那么每个人都会有一个结果。也许有一种方法可以做到每一次会话?你问“我需要计算平均会话时间:”和“有没有其他方法可以得到时间形式的结果(分和秒)?”这两个问题都得到了回答。考虑投票并接受答案。如果您有新问题-将其作为新问题发布,并附上所有相关细节,我们也会回答您的问题:o)