sql - 努力从 BigQuery SQL 中的时间戳字段中提取特定月份的 DATE
问题描述
本周刚开始我的第一份数据分析师工作!我的第一个 bigquery 遇到了很多问题:我需要以某种方式从时间戳字段中提取一组特定的日期。
具体来说: 我需要从时间戳数据中提取 12 月份的所有日期,因此 2020 年 12 月 1 日 - 2020 年 12 月 31 日的范围。我尝试了另外两个 stackoverflow 条目中的公式:
我忘了包括数据的外观:2020-12-12 16:36:58.944 UTC,只是想最终取回日期“2020-12-2-01 - 2020-12-31”。
1. 从 BigQuery 中的时间戳中提取日期:一种更可取的方法 2. BigQuery:从带有时区的日期时间中提取日期
我尝试的代码 SELECT EXTRACT(DATE FROM PARSE_TIMESTAMP('%m/%d/%Y %H:%M:%S %Z %z', '11/27/2019 14:40:15 CET +0100'))作为日期
我不明白如何仅提取时间戳的日期部分,然后如何仅提取或排列日期,以便我的查询仅返回 12 月份的值。
哈!我尝试使用 Extract 函数和 Trunc 函数,但没有任何效果。
我仍在学习提出问题的正确/最容易理解的方式,所以欢迎所有澄清的提示!
解决方案
考虑下面
select *,
from `project.dataset.table`
where date_trunc(date(parse_timestamp('%m/%d/%Y %H:%M:%S %Z %z',ts)),month)='2020-12-01'
要测试,玩上面你可以使用下面的玩具示例
with `project.dataset.table` as (
select 1 id, '11/27/2020 14:40:15 CET +0100' ts union all
select 2, '11/29/2020 14:40:15 CET +0100' union all
select 3, '11/30/2020 14:40:15 CET +0100' union all
select 4, '12/10/2020 00:20:15 CET +0100' union all
select 5, '12/20/2020 00:40:15 CET +0100' union all
select 6, '12/25/2020 14:40:15 CET +0100' union all
select 7, '12/27/2020 14:40:15 CET +0100' union all
select 8, '01/02/2021 14:40:15 CET +0100' union all
select 9, '01/04/2021 14:40:15 CET +0100'
)
select *,
date(parse_timestamp('%m/%d/%Y %H:%M:%S %Z %z', ts)) date,
date_trunc(date(parse_timestamp('%m/%d/%Y %H:%M:%S %Z %z', ts)), month) month
from `project.dataset.table`
where date_trunc(date(parse_timestamp('%m/%d/%Y %H:%M:%S %Z %z',ts)),month)='2020-12-01'
带输出
推荐阅读
- python-3.x - sum() 是否占用 python3 中循环的执行时间
- html - 使用 img src 的外部图像不允许整数参数
- sql-server - 让用户选择他想要多少天的数据
- fortran - 传递时字符串数组被无效
- keras - Keras-RL 集在拟合模型后返回相同的值
- c++ - 用于预填充运行时使用的对象的 ELF INIT 部分代码
- excel-formula - Excel 2007 INDEX/MATCH 在多个 IF 上使用 MIN
- android - 检查适用于 Android 的 Mobile Edge
- android - 如何在android中取消特定警报
- php - 如何从 db 获取上周和上个月的注册用户