Bigquery:如何将 2 个时间戳列合并为 1 个列?
Bigquery: How can I merge 2 timestamp columns into 1 column?
有谁知道如何从 Google Bigquery 中的两个时间戳列创建一个时间戳列?
我有一个带有两个时间戳列的 table,我想将这两列合并为一个列。 table 目前看起来像:
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
有谁知道如何从 Google Bigquery 中的两个时间戳列创建一个时间戳列?
我有一个带有两个时间戳列的 table,我想将这两列合并为一个列。 table 目前看起来像:
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