繁体   English   中英

Select BigQuery 中任意年份的给定月份和日期之间的日期

[英]Select where date between given month and day of any year in BigQuery

我有两个日期,我想根据日期制作一个 SELECT 声明。 假设日期是2019-09-142019-10-15

我知道我可以执行以下操作

SELECT date, amount, name FROM table WHERE name IN ('a', 'b') AND date BETWEEN '2019-09-14' AND '2019-10-15'

如果我想代替 select 9 月 14 日至 10 月 15 日之间的所有记录,而不考虑年份,该怎么办?

谢谢你。

这是使用简单算术的一种选择:

select *
from mytable
where 
    name in ('a', 'b')
    and extract(month from date) * 100 + extract(day from date) between 914 and 1015

这个想法是分别提取月份和日期,并将它们与一系列数字进行比较。 您还可以格式化日期并进行字符串比较:

select *
from mytable
where 
    name in ('a', 'b')
    and format_date('%m%d', date) between '0914' and '1015'

BigQuery 标准 SQL 的另一个选项

select date, amount, name 
from table 
where name in ('a', 'b') 
and date between date(extract(year from date), 9, 14) and date(extract(year from date), 10, 15)    

或者上面的冗长和bigquery'ish版本

select date, amount, name 
from table, unnest([extract(year from date)]) year 
where name in ('a', 'b') 
and date between date(year, 9, 14) and date(year, 10, 15)    

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM