Skip to content
Advertisement

Count days between dates based on multiple conditions

I have a table which has dates, IDs and states. Inside the postgres function I need to enter start_date and end_date. Now based on these entered dates, the count of the usage and available days should be returned as IDs -> count.

Example table:

Lets say I enter : start_date = ’01-01-2020′ and end_date = ’31-01-2020′

Output should be:

How its calculated :

  • For device 1: (2nd to 5th) = 3 days ; 6th to 31st = 26 day ; SO 26+3=29

  • For device 2: (21st to 31st) = 11 days

Advertisement

Answer

This works for me. dbfiddle

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement