【问题标题】:Can somebody help me in writing this SQL query in postgres有人可以帮我在 postgres 中编写这个 SQL 查询吗
【发布时间】:2016-11-07 13:44:05
【问题描述】:
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 函数不起作用,因为它说月份不存在。

帮助表示赞赏。

【问题讨论】:

  • 错误 错误:列“月”不存在第 2 行:...' 和 '2016-09-27' 按 SPARE_PART_ID,DATEADD(month, DAT. .. ^ ********** 错误 ********** 错误:列“月”不存在 SQL 状态:42703 字符:207
  • 这里你可以看到 MS SQL 和 Postgres 之间的变化:sqlines.com/postgresql/how-to/dateaddsqlines.com/postgresql/how-to/datediff
  • 你在哪里找到in the Postgres manual dateadd()datediff()
  • 您在group by clause 中使用dateadd() - 那应该做什么?
  • 按 SPARE_PART_ID 和 dateadd 分组

标签: postgresql-9.1


【解决方案1】:

Postgresql 中没有 DATEDIFF 函数,因此您会收到此错误。

您可以使用此查询获取月份的差异,而不是使用 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 中。

【讨论】:

  • 你能在我给定的查询中写上上面的查询吗,因为我的查询中只有一个日期......那是 created_dt
猜你喜欢
  • 1970-01-01
  • 2011-07-14
  • 1970-01-01
  • 2022-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-12
相关资源
最近更新 更多