Calculating difference of dates In Postgresql

后端 未结 3 1360
忘了有多久
忘了有多久 2021-01-17 15:34

I\'m trying to find out the time between certain fields in my tables. However cause I\'m using Postgresql :(( I can\'t use the DATEDIFF function. I can\'t f

相关标签:
3条回答
  • 2021-01-17 16:01

    You can use the age(<date1>, <date2>) function (instead of DATEDIFF).

    This should work -

    SELECT Question.ID, 
    Question.Status, COUNT (qUpdate.ID)  AS NumberofUpdates,
    age(Question.LoggedTime,MIN(qUpdate.UpdateTime)) AS TimeBeforeFirstUpdate,
    age(Question.LoggedTime, MAX(qUpdate.UpdateTime)) AS TimeBeforeLastUpdate
    FROM qUpdate
    LEFT JOIN Question ON qUpdate.qID=Question.ID
    WHERE Question.Status = 'closed' AND qUpdate.Update NOT NULL
    GROUP BY Question.Status, Question.ID, Question.LoggedTime;
    

    Note, if psql gives you this error - ERROR: date/time field value out of range, then you would need to choose an appropriate datestyle.

    0 讨论(0)
  • 2021-01-17 16:09
    SELECT extract(year from age('2014-01-23', '1985-08-27'));
    -- returns 28 years, my current age.
    
    0 讨论(0)
  • 2021-01-17 16:12

    You don't need a "datediff" function.

    Just subtract the two dates:

    Question.LoggedTime - MIN(qUpdate.UpdateTime)
    

    In case you don't know, but all that is documented online:
    http://www.postgresql.org/docs/current/static/functions-datetime.html

    0 讨论(0)
提交回复
热议问题