BigQuery:在30分钟时间戳间隔之间计数条目

huangapple go评论119阅读模式
英文:

BigQuery: counting entries between 30-minute timestamp intervals

问题

以下是翻译好的部分:

  1. 我有一个预先创建的表格 `data`

id timestamp_entry
1 "2023-01-01 04:11:24 UTC"
2 "2023-01-01 04:14:55 UTC"
...
99999 "2023-01-31 23:45:59 UTC"

  1. 其中 `timestamp_entry` 具有统一的时区 "UTC",时间范围在 2023 1 月内。
  2. 我想创建一个 30 分钟的时间框架,并计算 `timestamp_entry` 中有多少条目落入每个时间间隔内。
  3. 我首先创建了一个子查询:
  4. ```sql
  5. WITH
  6. intervals AS (
  7. SELECT interval AS start_time,
  8. TIMESTAMP_SUB(TIMESTAMP_ADD(interval, INTERVAL 30 MINUTE), INTERVAL 1 SECOND) AS end_time
  9. FROM UNNEST(GENERATE_TIMESTAMP_ARRAY("2023-01-01 00:00:00 UTC", "2023-01-31 23:59:59 UTC", INTERVAL 30 MINUTE)) interval
  10. )

但理想情况下,我希望我的结果显示:

  1. start_time end_time count
  2. "2023-01-01 00:00:00 UTC" "2023-01-01 00:29:59 UTC" 0
  3. "2023-01-01 00:30:00 UTC" "2023-01-01 00:59:59 UTC" 0
  4. ...
  5. "2023-01-31 23:00:00 UTC" "2023-01-31 23:29:59 UTC" 12
  6. "2023-01-31 23:30:00 UTC" "2023-01-31 23:59:59 UTC" 5

其中 count 显示了有多少条 data 中的 timestamp_entry 落入每个时间段。

我尝试使用 RIGHT JOINBETWEEN,但由于没有确切的 "匹配项",我无法将这两个表连接起来。

任何见解都将不胜感激。

  1. <details>
  2. <summary>英文:</summary>
  3. I have a pre-created table `data`:

id timestamp_entry
1 "2023-01-01 04:11:24 UTC"
2 "2023-01-01 04:14:55 UTC"
...
99999 "2023-01-31 23:45:59 UTC"

  1. where `timestamp_entry` has a uniform time zone &quot;UTC&quot; and ranges within January 2023.
  2. I want to create a 30-minute time skeleton and count how many entries in `timestamp_entry` falls into each interval.
  3. I first created a subquery:

WITH
intervals AS(
SELECT interval AS start_time,
TIMESTAMP_SUB(TIMESTAMP_ADD(interval, INTERVAL 30 MINUTE), INTERVAL 1 SECOND) AS end_time
FROM UNNEST(GENERATE_TIMESTAMP_ARRAY("2023-01-01 00:00:00 UTC", "2023-01-31 23:59:59 UTC", INTERVAL 30 MINUTE)) interval
)

  1. But ideally, I want my outcome to show:

start_time end_time count
"2023-01-01 00:00:00 UTC" "2023-01-01 00:29:59 UTC" 0
"2023-01-01 00:30:00 UTC" "2023-01-01 00:59:59 UTC" 0
...
"2023-01-31 23:00:00 UTC" "2023-01-31 23:29:59 UTC" 12
"2023-01-31 23:30:00 UTC" "2023-01-31 23:59:59 UTC" 5

  1. where `count` shows how many `timestamp_entry` from `data` falls into each interval.
  2. I have tried using `RIGHT JOIN` with `BETWEEN`, but I won&#39;t be able to join the two tables as there are no exact &quot;matches&quot;.
  3. Any insights are appreciated.
  4. </details>
  5. # 答案1
  6. **得分**: 0
  7. 以下是代码部分的中文翻译:
  8. ```sql
  9. 与 `JOIN` 和 `BETWEEN` 完全匹配的操作:
  10. SELECT start_time, end_time, COUNT(*) AS count
  11. FROM data
  12. LEFT JOIN intervals
  13. ON timestamp_entry >= start_time
  14. AND timestamp_entry < end_time
  15. GROUP BY start_time, end_time
  16. ORDER BY start_time
英文:

Works exactly with JOIN and BETWEEN:

  1. SELECT start_time, end_time, COUNT(*) AS count
  2. FROM data
  3. LEFT JOIN intervals
  4. ON timestamp_entry &gt;= start_time
  5. AND timestamp_entry &lt; end_time
  6. GROUP BY start_time, end_time
  7. ORDER BY start_time

答案2

得分: 0

  1. WITH data AS (
  2. -- 在此处放入你的数据
  3. )
  4. SELECT TIMESTAMP_SECONDS(slot) start_time,
  5. TIMESTAMP_SECONDS(slot + 1800 - 1) end_time,
  6. COUNT(timestamp_entry) `count`
  7. FROM UNNEST(GENERATE_ARRAY(1672531200, 1675207800, 1800)) slot
  8. LEFT JOIN data ON DIV(slot, 1800) = DIV(UNIX_SECONDS(timestamp_entry), 1800)
  9. GROUP BY 1, 2;
  • 1672531200 -> UNIX_SECONDS("2023-01-01 00:00:00 UTC");
  • 1675207800 -> UNIX_SECONDS("2023-01-31 23:30:00 UTC");
英文:

You can consider below as well

  1. WITH data AS (
  2. -- put your data here
  3. )
  4. SELECT TIMESTAMP_SECONDS(slot) start_time,
  5. TIMESTAMP_SECONDS(slot + 1800 - 1) end_time,
  6. COUNT(timestamp_entry) `count`
  7. FROM UNNEST(GENERATE_ARRAY(1672531200, 1675207800, 1800)) slot
  8. LEFT JOIN data ON DIV(slot, 1800) = DIV(UNIX_SECONDS(timestamp_entry), 1800)
  9. GROUP BY 1, 2;
  • 1672531200 -> UNIX_SECONDS("2023-01-01 00:00:00 UTC");
  • 1675207800 -> UNIX_SECONDS("2023-01-31 23:30:00 UTC");

huangapple
  • 本文由 发表于 2023年3月9日 20:41:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75684766.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定