SQL 雅典娜大于日期列中的条件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51269919/
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-08-31 23:30:12  来源:igfitidea点击:

Athena greater than condition in date column

sqlamazon-web-servicesamazon-athenapresto

提问by efbbrown

I have the following query that I am trying to run on Athena.

我尝试在 Athena 上运行以下查询。

SELECT observation_date, COUNT(*) AS count
FROM db.table_name
WHERE observation_date > '2017-12-31'
GROUP BY observation_date

However it is producing this error:

但是它产生了这个错误:

SYNTAX_ERROR: line 3:24: '>' cannot be applied to date, varchar(10)

This seems odd to me. Is there an error in my query or is Athena not able to handle greater than operators on date columns?

这对我来说似乎很奇怪。我的查询中是否存在错误,或者 Athena 是否无法处理日期列上的大于运算符?

Thanks!

谢谢!

回答by Barry Piccinni

You need to use a cast to format the date correctly before making this comparison. Try the following:

在进行此比较之前,您需要使用强制转换来正确设置日期格式。请尝试以下操作:

SELECT observation_date, COUNT(*) AS count
FROM db.table_name
WHERE observation_date > CAST('2017-12-31' AS DATE)
GROUP BY observation_date

Check it out in Fiddler: SQL Fidle

在 Fiddler 中查看:SQL Fiddle

UPDATE 17/07/2019

更新 17/07/2019

In order to reflect comments

为了反映评论

SELECT observation_date, COUNT(*) AS count
FROM db.table_name
WHERE observation_date > DATE('2017-12-31')
GROUP BY observation_date

回答by Zerodf

You can also use the datefunction which is a convenient alias for CAST(x AS date):

您还可以使用date函数,这是一个方便的别名CAST(x AS date)

SELECT * 
FROM date_data
WHERE trading_date >= DATE('2018-07-06');