How can I calculate the interval between two datetime values, but only count business days (Monday to Friday) using PostgreSQL? Currently, my query is not returning the expected result, which should include the number of days in addition to hours, minutes, and seconds.
WITH cte_temp AS (
SELECT
id,
date_create,
(SELECT
('2023-02-28 23:59:59'::timestamp - date_create::timestamp)
- INTERVAL '1 day' * (EXTRACT(EPOCH FROM ('2023-02-28 23:59:59'::timestamp - date_create::timestamp))::int / 86400)
) AS interval_business
FROM
req
WHERE
(date_create::timestamp <= CONCAT('2023-02-28',' 23:59:59')::timestamp)
)
SELECT
*
FROM
cte_temp
Result :
id | date_create | interval_business |
---|---|---|
1 | 2023-02-15T12:45:10.000Z | {"hours":11,"minutes":14,"seconds":49} |
The expected result should be :
id | date_create | interval_business |
---|---|---|
1 | 2023-02-15T12:45:10.000Z | {"days":11,"hours":11,"minutes":14,"seconds":49} |
DB fidle : https://www.db-fiddle.com/f/9cRUog6XGRadVsW6LWogrv/0
EDIT :
My database is readonly