Find the last weekday for a given month in PostgreSQL
Usage: If month end falls on a Saturday or a Sunday, return the previous Friday, else use month end
Examples:
- 3/31/2013 falls on a Sunday, so return 3/29/2013
- 11/30/2013 falls on a Saturday, so return 11/29/2013
How to write this in PostgreSQL SQL?
What I have so far is this (returns only Month Ends, but month ends don't exist when they fall on a Saturday or Sunday):
SELECT as_of_dt, sum(bank_shr_bal) as bank_shr_bal
FROM hm_101.vw_gl_bal
WHERE as_of_dt = (date_trunc('MONTH', as_of_dt) + INTERVAL '1 MONTH - 1 day')::date
GROUP BY 1
Thanks