Ok here is some example data and the expected results I would like:
- 2011-06-26 17:07:38-04 <– Start Date
- 2011-10-01 00:00:00-04 <– Wanted Date
-
91 <– Duration in days
-
2011-06-25 20:08:46-04 <– Start Date
- 2011-09-01 00:00:00-04 <– Wanted Date
- 62 <– Duration in days
Here are the conditions:
- If the day the 1st to the 25th, add two months plus how many days until the next first
- If the day is the 26th till the end of the month, add three months plus how many days until the next first
Here is what I’m trying and I’m close but this seems to be doing a ton of calculations and I wanted to see if I could simplify it:
CASE
WHEN (((DATE_TRUNC('month',
(date_field + INTERVAL '5 DAYS')) + INTERVAL '2 MONTH') -
(date_field + INTERVAL '5 DAYS') >= 62) AND
((DATE_TRUNC('month', (date_field + INTERVAL '5 DAYS')) +
INTERVAL '2 MONTH') - (date_field + INTERVAL '5 DAYS') <= 92))
THEN (DATE_TRUNC('month',
(date_field + INTERVAL '5 DAYS')) + INTERVAL '2 MONTH')
ELSE (DATE_TRUNC('month',
(date_field + INTERVAL '5 DAYS')) + INTERVAL '3 MONTH')
END
1 Answer