在自定义查询中查询数据时格式化 event_date 和 event_timestamp 大查询

Format event_date and event_timestamp Big query while querying data in custom query

您好,以下是我获取一些大数据的查询 query 想知道我们如何格式化 datetime stamp

SELECT
  *
FROM (
  SELECT
    (
    SELECT
      x.value
    FROM
      UNNEST(user_properties) x
    WHERE
      x.key='restaurantName'
      AND x.value IS NOT NULL).string_value AS restaurantName,
    event_date AS date,
    event_timestamp AS time,
    event_name AS Event,
    (
    SELECT
      x.value
    FROM
      UNNEST(user_properties) x
    WHERE
      x.key='restaurantId'
      AND x.value IS NOT NULL).string_value AS restaurantID,
    (
    SELECT
      x.value
    FROM
      UNNEST(user_properties) x
    WHERE
      x.key='user_id'
      AND x.value IS NOT NULL).string_value AS user
  FROM
    `analytics.events_*`
  WHERE
    event_name = "OrderSummary"
    AND app_info.id = "app_Id"
  ORDER BY
    event_timestamp ASC)
WHERE
  NOT(restaurantName IS NULL
    OR restaurantID="someName")

我正在正确过滤数据,但无法格式化

转换为TIMESTAMP,然后FORMAT_TIMESTAMP()

WITH data AS (SELECT 1574914933030017 ms)


SELECT TIMESTAMP_MICROS(ms)
  , FORMAT_TIMESTAMP('%d/%m/%Y %H:%M', TIMESTAMP_MICROS(ms))
FROM data


2019-11-28 04:22:13.030017 UTC
28/11/2019 04:22

format you expect to have in output? date = 28-11-2019 and time = 04:22:13

以下适用于 BigQuery 标准 SQL

Change/fix 就在下面两行

event_date AS date,
event_timestamp AS time,  

所以,而不是上面 - 使用下面

FORMAT_DATE('%d-%m-%Y', PARSE_DATE('%Y%m%d', event_date)) AS date, 
FORMAT_TIME('%T', TIME(TIMESTAMP_MICROS(event_timestamp))) time,   

并且相应的输出列将类似于

Row date        time     
1   28-11-2019  04:22:13     

注意:我假设 event_date 字段是 STRING 数据类型。如果它实际上是一个 INT64 - 你只需要使用 CAST(event_date AS STRING) 而不是 event_date