当前位置:  开发笔记 > 编程语言 > 正文

SQL在BigQuery中将yyyymmdd转换为时间戳?

如何解决《SQL在BigQuery中将yyyymmdd转换为时间戳?》经验,为你挑选了1个好方法。



1> Elliott Bros..:

您可以使用PARSE_DATEPARSE_TIMESTAMP(格式字符串将相同)来获取日期或时间戳。例如:

SELECT
  d,
  PARSE_DATE('%Y%m%d', d) AS date,
  PARSE_TIMESTAMP('%Y%m%d', d) AS timestamp
FROM UNNEST(['20170117', '20161231']) AS d;

您可以在文档中阅读有关格式字符串的更多信息。有日期和时间戳部分,分别适用于PARSE_DATEPARSE_TIMESTAMP

编辑:从更新的问题中,您可以将查询更改为以下内容:

WITH allTables as (
  SELECT
      event.date as date,
      count(*) as totalSessions,
      count(DISTINCT user_dim.app_info.app_instance_id) as uniqueUsers
  FROM `namehiddenonlyhere.*`
  CROSS JOIN
      UNNEST(event_dim) AS event
  WHERE
      event.name = 'session_start'
  AND 
      event.date <= FORMAT_DATE('%Y%m%d', DATE_SUB(CURRENT_DATE, INTERVAL 1 DAY))
  AND
      event.date >= FORMAT_DATE('%Y%m%d', DATE_SUB(CURRENT_DATE, INTERVAL 7 DAY))
  GROUP BY event.date

  UNION ALL 
  SELECT
      event.date as date,
      count(*) as totalSessions,
      count(DISTINCT user_dim.app_info.app_instance_id) as uniqueUsers
  FROM `namehiddenonlyhere.*`
  CROSS JOIN
      UNNEST(event_dim) AS event
  WHERE
      event.name = 'session_start'
  AND 
      event.date <= FORMAT_DATE('%Y%m%d', DATE_SUB(CURRENT_DATE, INTERVAL 1 DAY))
  AND
      event.date >= FORMAT_DATE('%Y%m%d', DATE_SUB(CURRENT_DATE, INTERVAL 7 DAY))
  GROUP BY event.date
) SELECT 'all apps' as target, PARSE_DATE('%Y%m%d', date) as datapoint_time, totalSessions datapoint_value FROM allTables;

我所做的唯一修改是更改:

date AS datapoint_time

至:

PARSE_DATE('%Y%m%d', date) as datapoint_time

推荐阅读
135369一生真爱_890
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有