google-bigquery - Bigquery:如何将 2 个时间戳列合并为 1 个列?
问题描述
有谁知道如何从 Google Bigquery 中的两个时间戳列创建单个时间戳列?
我有一个包含两个时间戳列的表,我想将这两列合并到一个列中。该表目前看起来像:
id | user_id | created_at_a | created_at_b
------------------------------------------------------------------
1 | 1 | 2019-01-24 12:20:00 UTC | 2019-01-25 01:04:00 UTC
2 | 1 | 2019-01-24 12:20:00 UTC | 2019-01-25 01:03:00 UTC
3 | 1 | 2019-01-24 12:22:00 UTC | 2019-01-25 01:03:00 UTC
4 | 1 | 2019-01-24 12:22:00 UTC | 2019-01-25 01:04:00 UTC
5 | 2 | 2019-01-24 20:48:00 UTC | 2019-01-24 20:49:00 UTC
6 | 2 | 2019-01-24 11:21:00 UTC | 2019-01-24 20:49:00 UTC
所以...我正在尝试将这两个时间戳列合并为一列。我的预期结果如下:
id | user_id | created_at_a
----------------------------------------
1 | 1 | 2019-01-24 12:20:00 UTC
2 | 1 | 2019-01-25 01:04:00 UTC
4 | 1 | 2019-01-25 01:03:00 UTC
5 | 1 | 2019-01-24 12:22:00 UTC
6 | 2 | 2019-01-24 20:48:00 UTC
7 | 2 | 2019-01-24 20:49:00 UTC
8 | 2 | 2019-01-24 11:21:00 UTC
有人可以帮我吗。
非常感谢!
解决方案
以下是 BigQuery 标准 SQL
#standardSQL
SELECT DISTINCT user_id, created_at
FROM (
SELECT user_id,
ARRAY_CONCAT_AGG([created_at_a, created_at_b]) created_at_ab
FROM `project.dataset.table`
GROUP BY user_id
), UNNEST(created_at_ab) created_at
您可以使用您问题中的示例数据进行测试,如下所示
#standardSQL
WITH `project.dataset.table` AS (
SELECT 1 id, 1 user_id, TIMESTAMP '2019-01-24 12:20:00 UTC' created_at_a, TIMESTAMP '2019-01-25 01:04:00 UTC' created_at_b UNION ALL
SELECT 2, 1, '2019-01-24 12:20:00 UTC', '2019-01-25 01:03:00 UTC' UNION ALL
SELECT 3, 1, '2019-01-24 12:22:00 UTC', '2019-01-25 01:03:00 UTC' UNION ALL
SELECT 4, 1, '2019-01-24 12:22:00 UTC', '2019-01-25 01:04:00 UTC' UNION ALL
SELECT 5, 2, '2019-01-24 20:48:00 UTC', '2019-01-24 20:49:00 UTC' UNION ALL
SELECT 6, 2, '2019-01-24 11:21:00 UTC', '2019-01-24 20:49:00 UTC'
)
SELECT DISTINCT user_id, created_at
FROM (
SELECT user_id,
ARRAY_CONCAT_AGG([created_at_a, created_at_b]) created_at_ab
FROM `project.dataset.table`
GROUP BY user_id
), UNNEST(created_at_ab) created_at
-- ORDER BY user_id, created_at
结果
Row user_id created_at
1 1 2019-01-24 12:20:00 UTC
2 1 2019-01-24 12:22:00 UTC
3 1 2019-01-25 01:03:00 UTC
4 1 2019-01-25 01:04:00 UTC
5 2 2019-01-24 11:21:00 UTC
6 2 2019-01-24 20:48:00 UTC
7 2 2019-01-24 20:49:00 UTC
推荐阅读
- django - 如何在 django 中修改继承类中的登录视图?
- python - Pandas:如何按日期时间列分组,仅使用时间并丢弃日期
- html - CSS 位置 Sticky、Z-Index 和绝对定位不起作用...如何解决这个问题?
- angular - 在融合图表Angular 8中添加数据加载消息
- telegram-bot - 电报机器人:继续私下对话
- html - 链接到表格 HTML / Vuetify.js 中的特定行
- ios - 尝试从 UIImage 剪辑边缘以适合 ImageView
- r - 在零之前插入一个空白行
- docker - 将 docker-machine 作为 snap 与标准安装运行
- mysql - SQL:从右边开始的子字符串