Calculate difference based on timestamp



  • in my PostgreSQL DB I have got table like that:

    calendar_tab
    calendar_id - int
    user_id - int
    type - varchar(16)
    start_date - timestamp
    end_date - timestamp
    

    Users add different events to this table. One of type is "AVAIL" which means "availability".

    stat_date and end_date means when user is available.

    On the other hand the they also add events with different type for example "LESSON".

    I'm looking for some sql function that will return user availability base on start and end timestamp.

    get_availability(start, end)
    

    function should calculate difference between type=AVAIL and all others per user.

    I'm not sure how to calculate that difference.

    Thanks for advice



  • You can use the AGE function. For more details check https://www.postgresql.org/docs/current/functions-datetime.html




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2