【问题标题】:how to get number of days between two different dates from different tables in sql?如何从sql中的不同表中获取两个不同日期之间的天数?
【发布时间】:2021-08-10 06:30:25
【问题描述】:

我需要估计来自不同表格的 2 个不同日期之间的天数。 我曾尝试使用datediff 函数,但它不起作用。

这是我尝试过的查询:

select 
   t1."group",
   datediff(t2.start_date, t1.event_date),
   count(distinct t1.employee_id)
from t1 join t2 on t1.project_id = t2.id
where true
  and t1.cummulative_manhour > 0
  and t1.company_id in ('a', 'b')
  and t1.event_date = '2021-05-17'
  and t1.project_id = '2900'
group by 
  t1."group",
  t2.start_date,
  t1.event_date

错误信息说:

SQL Error [42883]: ERROR: function datediff(date, date) does not exist
  Hint: No function matches the given name and argument types. You might need to add explicit type casts.
  Position: 29

我使用的数据库是postgresql。

我应该如何编写查询以获得预期的天数?

提前致谢。

【问题讨论】:

  • 为什么你认为 Postgres 有datediff() 功能? in the manual 没有任何建议。

标签: sql postgresql datediff


【解决方案1】:

正如错误消息所示,Postgres 不支持datediff()。相反,它只使用-:

(t2.start_date - t1.event_date),

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-20
    • 1970-01-01
    • 2020-04-12
    • 2021-07-24
    • 2023-03-31
    • 2018-06-15
    • 1970-01-01
    相关资源
    最近更新 更多