努力从 BigQuery SQL 的时间戳字段中提取特定月份的 DATE(s)

问题描述

本周刚开始我的第一份数据分析师工作! 我的第一个 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. Extracting date from timestamp in Bigquery: a preferable method 2. BigQuery: extract date from datetime with timezone

我尝试过的代码 选择 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',month) month
from `project.dataset.table`
where date_trunc(date(parse_timestamp('%m/%d/%Y %H:%M:%S %Z %z',month)='2020-12-01'    

带输出

enter image description here