2016-11-07 65 views
1
select SPARE_PART_ID amont_id,sum(amount) monthAmount 
from pmms.SPARE_PART_TRANS_INFO 
where trans_type='2' 
and CAST(created_dt AS date) between '2016-09-19' and '2016-09-27' 
group by SPARE_PART_ID,DATEADD(month, DATEDIFF(month, 0, CAST(created_dt AS date)), 0) 

在这个查询中DATEADD和DATEDIFF函数不工作,因为它说的月份不存在。有人可以帮我写postgres中的这个SQL查询

帮助赞赏。

+0

**错误**错误:列 “月” 不存在 LINE 2:SPARE_PART_ID,DATEADD(月,DAT ... ^ ...”和 '2016年9月27日' 组** ********错误********** 错误:列“月”不存在 SQL状态:42703 字符:207 –

+0

在这里您可以看到MS SQL和Postgres之间的变化:http://www.sqlines.com/postgresql/how-to/dateadd http://www.sqlines.com/postgresql/how-to/datediff – McNets

+0

其中[在Postgres手册](http:// www。 postgresql.org/docs/current/static/index.html)你找到了'dateadd()'或'datediff()' –

回答

0

DATEDIFF函数不在Postgresql中,因此你得到这个错误。

除了使用DATEDIFF,你可以使用此查询

SELECT (DATE_PART('year', '2012-01-01'::date) - DATE_PART('year', '2011-10-02'::date)) * 12 + 
       (DATE_PART('month', '2012-01-01'::date) - DATE_PART('month', '2011-10-02'::date)); 

检查得到的月差为details在上面的查询,这将解释DATEDIFF不是在PostgreSQL中的使用。

+0

你可以请在我的查询中写上面的查询,因为我只有一个日期在我的查询...这是created_dt –