列字符串到时间

发布于 2025-02-01 23:00:42 字数 109 浏览 4 评论 0原文

SQL的新事物!我使用BigQuery。 如何将整个字符串列转换为时间? 列名是ride_length,其格式为00:00(分钟:秒)。 架构是字符串,但我需要时间。 那我该如何计算平均水平? 先感谢您。

New in SQL here! I use BigQuery.
How do I convert an entire string column to time?
The column name is ride_length and it's format is 00:00 (minutes:seconds).
On schema is string but I need it to be time.
Also how can I calculate its average then?
Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只怪假的太真实 2025-02-08 23:00:42

我认为有多种方法可以做到这一点。但是,这完全取决于您如何需要最终结果(甚至处理中间值)。一种方法是,您可以使用Split,Cast&可能计算总数添加,进行平均&将其放回时间数据类型(或以分钟/秒的时间维护)

WITH
  s AS (
  SELECT
    '45:00' AS time
  UNION ALL
  SELECT
    '30:00' AS time
  UNION ALL
  SELECT
    '20:00' AS time
  UNION ALL
  SELECT
    '25:00' AS time
  UNION ALL
  SELECT
    '04:00' AS time
  UNION ALL
  SELECT
    '25:30' AS time )
SELECT
  avg_s,
  time(TIMESTAMP_SECONDS(avg_s))
FROM (
  SELECT
    CAST(AVG(secs) AS INT64) AS avg_s
  FROM (
    SELECT
      time,
      SAFE_CAST(SPLIT(time, ':') [ORDINAL(1)] AS INT64)*60+ SAFE_CAST(SPLIT(time, ':') [ORDINAL(2)] AS INT64) AS secs
    FROM
      s ) )

I think there are multiple ways to do it. But, it totally depends on how you need the end result (or even process the intermediate values). One way is you could possibly calculate the total seconds using split, cast & add, do the average & cast it back to time datatype (or maintain in minutes/seconds)

WITH
  s AS (
  SELECT
    '45:00' AS time
  UNION ALL
  SELECT
    '30:00' AS time
  UNION ALL
  SELECT
    '20:00' AS time
  UNION ALL
  SELECT
    '25:00' AS time
  UNION ALL
  SELECT
    '04:00' AS time
  UNION ALL
  SELECT
    '25:30' AS time )
SELECT
  avg_s,
  time(TIMESTAMP_SECONDS(avg_s))
FROM (
  SELECT
    CAST(AVG(secs) AS INT64) AS avg_s
  FROM (
    SELECT
      time,
      SAFE_CAST(SPLIT(time, ':') [ORDINAL(1)] AS INT64)*60+ SAFE_CAST(SPLIT(time, ':') [ORDINAL(2)] AS INT64) AS secs
    FROM
      s ) )

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文