首页 > 解决方案 > 按月获取错误过滤记录SQL

问题描述

我有以下代码,我试图在其中查找 May 的记录并尝试按网站对它们进行分组。但是得到这个错误。知道为什么它说没有功能匹配吗?

SELECT
ad.site, SUM(e.clicks) AS totalclicks
FROM Extable e 
INNER JOIN Ads ad ON e.ad_id = ad.ad_id
WHERE month(ad.Create_Time) = 5
Group by ad.site;

但我收到了这个错误

ERROR: function month(date) does not exist
LINE 4: WHERE month(ad.Create_Time) = 5
^

HINT: No function matches the given name and argument types. You might need to add explicit type casts.

SQL state: 42883
Character: 107

标签: sqlpostgresql

解决方案


是 PostgreSQL 的日期/时间函数的文档。似乎您必须使用 EXTRACT() 或 DATE_PART()

date_part('hour', timestamp '2001-02-16 20:38:40')

extract(hour from timestamp '2001-02-16 20:38:40')

推荐阅读