BigQuery SQL WHERE 当前日期和 -15 天之间的日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41243816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 05:03:15  来源:igfitidea点击:

BigQuery SQL WHERE Date Between Current Date and -15 Days

sqlgoogle-bigquery

提问by Eric Hendershott

I am trying to code the following condition in the WHEREclause of SQL in BigQuery, but I am having difficulty with the syntax, specifically datemath:

我试图WHERE在 BigQuery 的 SQL 子句中编写以下条件,但我在语法上遇到困难,特别是日期数学:

WHERE date_column between current_date() and current_date() - 15 days

This seems easy in MySQL, but I can't get it to work with BigQuery SQL.

这在 MySQL 中似乎很容易,但我无法让它与 BigQuery SQL 一起使用。

回答by JohnHC

Use DATE_SUB

使用DATE_SUB

select * 
from TableA
where Date_Column between DATE_SUB(current_date(), INTERVAL 15 DAY) and current_date()

Remember, betweenneeds the oldest date first

请记住,首先between需要最旧的日期

回答by Siyual

You should probably switch the two around - the syntax should be the following:

您可能应该切换两者 - 语法应如下所示:

WHERE date_column BETWEEN DATE_ADD(CURRENT_DATE(), -15, 'DAY') AND CURRENT_DATE()

回答by ppk28

This works for me.

这对我有用。

WHERE DATE(date_column) BETWEEN DATE(DATE_ADD(CURRENT_DATE(), -15, 'DAY'))
AND CURRENT_DATE()

回答by KARTHIKEYAN.A

Use current_date()function

使用current_date()函数

SELECT ARRAY_TO_STRING(split(cast(current_date() as string),"-"),'')