【问题标题】:How do you I calculate the difference between two timestamps in PostgreSQL?我如何计算 PostgreSQL 中两个时间戳之间的差异?
【发布时间】:2020-10-05 12:25:41
【问题描述】:

我有一个 SELECT 查询来检索我需要的数据。所需数据之一是“聚合”函数,用于选择两个时间戳日期并计算它们之间的差异。 使用了几个资源和网站,他们都给了我答案,但没有用:

DECLARE @DATE1 TIMESTAMP = actual_route.end_location_time
DECLARE @DATE2 TIMESTAMP = actual_route.actual_trip_start_time
SELECT 
    CONVERT (TIMESTAMPDIFF, @DATE1, @DATE2) AS time_taken, actual_route.time as date_time

我在“@”处遇到错误:“@”处或附近的语法错误 (这只是代码的一部分,但错误在这里)

【问题讨论】:

标签: sql postgresql timestamp aggregate-functions olap-cube


【解决方案1】:

您可以在 Postgres 中获取差异:

select (actual_route.end_location_time - actual_route.actual_trip_start_time) as duration

我不确定您为什么要为此使用 SQL Server 语法。我也不知道timestampdiff 应该是什么。

【讨论】:

  • 我正在创建一个 OLAP 多维数据集,其中两个时间戳之间的差异是多维数据集的度量
  • “timestamdiff”函数是我在网上找到的
  • @JanaNizam 。 . .尽管在某些数据库中这是一个函数,但我从未将其视为 convert() 的参数。
猜你喜欢
  • 2020-05-13
  • 2014-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-26
相关资源
最近更新 更多