您可以使用PARSE_DATE
或PARSE_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_DATE
和PARSE_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