Need to find the difference in hours between two dates along with the timestamp but I need to exclude saturday and sunday. Can you please help on this.
Thanks in Advance!!
Need to find the difference in hours between two dates along with the timestamp but I need to exclude saturday and sunday. Can you please help on this.
Thanks in Advance!!
SELECT
COUNT(*) * 24 -- 3
FROM generate_series(start, end, interval '1 day') gs -- 1
WHERE EXTRACT('isodow' FROM gs) < 6 -- 2
isodow (Monday = 1, Sunday = 7) and remove all records with isodow value < 6COUNT the records delivers the number of remaining dates/records, multiply it by 24 to get the hours.Edit (Question more specific in comments):
To calculate the interval with different start and end times you could do:
SELECT
COUNT(*) * interval '1 day' -- duration of days exclude start and end
+ CASE -- add start if not weekend
WHEN EXTRACT('isodow' FROM start) < 6
THEN (start::date + 1) - start
END
+ CASE -- add end if not weekend
WHEN EXTRACT('isodow' FROM "end") < 6
THEN "end" - "end"::date
END
FROM generate_series(
start::date + 1 ,
"end"::date - 1,
interval '1 day'
) gs
WHERE EXTRACT('isodow' FROM gs) < 6
GROUP BY start, "end"