我正试图计算Postgres上两个日期之间的差异。首先我得到日期:
SELECT * FROM
(
SELECT
to_char(to_timestamp(t.time/1000000), 'DD/MM/YYYY HH24:MI:SS') AS initial_date
FROM
book.ticket AS t
WHERE status = 'planning'
) AS initial_date,
(
SELECT
to_char(to_timestamp(t.time/1000000), 'DD/MM/YYYY HH24:MI:SS') AS final_date
FROM
book.ticket AS t
WHERE status = 'closed'
) AS final_date
然后我想做类似的事情来计算之前恢复日期之间的差异:
SELECT DATE_PART('day', final_date::timestamp - initial_date::timestamp)
但我不知道如何将这个最后一部分(那个计算的部分)和其他查询放在一起。有人能帮我吗?
好的,伙计们!经过所有这些讨论,我解决了我的问题。我将发布下面的代码。感谢您的帮助,每个人的意见都很重要!
SELECT t1.id, t1.initial_date, t2.id, t2.final_date, t2.final_date - t1.initial_date as dif
FROM (
(
SELECT
t.id,
to_timestamp(t.time/1000000) AS initial_date
FROM
book.ticket AS t
WHERE status = 'planning'
) t1
JOIN (
SELECT
t.id,
to_timestamp(t.time/1000000) AS final_date
FROM
book.ticket AS t
WHERE status = 'closed'
) t2
ON t1.id = t2.id
)