在 BigQuery SQL for Data Studio 中格式化 Firestore 时间戳

von*_*sch 2 sql google-bigquery google-data-studio google-cloud-firestore

我们将 Firestore 数据导入 BigQuery 以在数据洞察中生成报告。

以下适用于 SQL 时间戳,但不适用于 Firestore-JSON 时间戳。

SELECT 
    PARSE_TIMESTAMP('%Y%m%d', JSON_VALUE(`data`, "$.updated_at")) AS `updated_at`
FROM 
    `project.firestore_export.profiles_raw_latest`
Run Code Online (Sandbox Code Playgroud)

我们的日期在名为 的 JSON 字段timestamp的列中采用 Firestore格式。dataupdated_at

如何获得最终在 Data Studio 中使用的可用日期格式?

编辑:当我查询没有JSON_VALUE它返回的字段时null,它是 Firestore 中的标准时间戳格式。当我在 BigQuery 中预览数据时,它会以 JSON 对象的形式返回:{"_seconds":1569585420,"_nanoseconds":586000000}

Yun*_*ang 5

假设您不需要亚秒级精度,那么您可以使用:

#standardSQL
with sample_table as (
  select '{"_seconds":1569585420,"_nanoseconds":586000000}' as ts
  )
select PARSE_DATETIME("%s", JSON_EXTRACT_SCALAR(ts, "$['_seconds']"))
from sample_table;
Run Code Online (Sandbox Code Playgroud)

输出:

+---------------------+
|         f0_         |
+---------------------+
| 2019-09-27T11:57:00 |
+---------------------+
Run Code Online (Sandbox Code Playgroud)

如果您确实需要更精确的时间戳,您可以使用timestamp_micros()