简体   繁体   English

BigQuery 如何计算平均 DATETIME_DIFF

[英]BigQuery how to calculate average DATETIME_DIFF

I just can't figure out how to get the average of travel time ended_at and started_at using DATETIME_DIFF.我只是不知道如何使用 DATETIME_DIFF 获得旅行时间的平均值 ended_at 和 started_at。 So there are two possible values in column member_casual and I want to figure out how to group the average travel times per member group.因此,member_casual 列中有两个可能的值,我想弄清楚如何对每个成员组的平均旅行时间进行分组。 ie. IE。 return two rows with one value on each row, the average travel time for the group.返回两行,每行一个值,即该组的平均旅行时间。 I've tried searching but I've failed to translate the solutions to my issue.我试过搜索,但未能将解决方案转化为我的问题。

SELECT 
  member_casual,
  DATETIME_DIFF(started_at, ended_at, MINUTE) +
  CASE WHEN ended_at < started_at THEN 24 ELSE 0 end
FROM dataset
GROUP BY
  member_casual,
  started_at,
  ended_at
LIMIT 100

I've tried adding AVG(...) in several places but I guess I just don't know enough about SQL yet to figure this out.我试过在几个地方添加 AVG(...) 但我想我对 SQL 还不够了解,还没有弄清楚。

CASE is used to fix the error that happens when the travel period passes midnight. CASE 用于修复旅行时间超过午夜时发生的错误。

Basically you only add an average aroud the date_diff and the query will get the aveage for every member_casual基本上你只在 date_diff 周围添加一个平均值,查询将获得每个 member_casual 的平均值

also a LIMIT with Out order By makes no sense Out order By 的 LIMIT 也是没有意义的

SELECT 
  member_casual,
  AVG(DATETIME_DIFF(started_at, ended_at, MINUTE) +
  CASE WHEN ended_at < started_at THEN 24 ELSE 0 end) avd_date_diff
FROM dataset
GROUP BY
  member_casual
ORDER BY member_casual
LIMIT 100

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM