如何计算PostgreSQL中的DATE差异? [英] How to calculate DATE Difference in PostgreSQL?

查看:91
本文介绍了如何计算PostgreSQL中的DATE差异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,我需要计算 PostgreSQL 中两个日期的差。

Here I need to calculate the difference of the two dates in the PostgreSQL.

在SQL Server中:就像我们在 SQL Server 中所做的那样,<

In SQL Server: Like we do in SQL Server its much easier.

DATEDIFF(Day, MIN(joindate), MAX(joindate)) AS DateDifference;

我的尝试:我正在尝试使用以下脚本:

My Try: I am trying using the following script:

(Max(joindate) - Min(joindate)) as DateDifference;

问题


  • 我的方法正确吗?

  • Is my method correct?

PostgreSQL中是否有任何功能 code>来计算这个?

Is there any function in PostgreSQL to calculate this?

推荐答案

您的计算正确表示 DATE 类型,但是如果您的值是时间戳,则应该使用 EXTRACT (或DATE_PART)以确保仅获得整天天;

Your calculation is correct for DATE types, but if your values are timestamps, you should probably use EXTRACT (or DATE_PART) to be sure to get only the difference in full days;

EXTRACT(DAY FROM MAX(joindate)-MIN(joindate)) AS DateDifference

要测试的SQLfiddle 。请注意,时间戳记差异要比整整2天少1秒。

An SQLfiddle to test with. Note the timestamp difference being 1 second less than 2 full days.

这篇关于如何计算PostgreSQL中的DATE差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆