Previous Quarter three months of Start Date and End Dates [message #690125] |
Wed, 16 October 2024 12:35 |
|
suji6281
Messages: 151 Registered: September 2014
|
Senior Member |
|
|
Hi All,
Can you please help me with the SQL with the following scenarios.
1. SQL should return previous three months start and end dates when current month is APR.
Output:
1-Jan-2024, 31-Jan-2024
1-Feb-2024, 28-Feb-2024
1-Mar-2024, 31-Mar-2024
2. SQL should return previous three months start and end dates when current month is JUL.
Output:
1-Apr-2024, 30-Apr-2024
1-May-2024, 31-May-2024
1-Jun-2024, 30-Jun-2024
3. SQL should return previous three months start and end dates when current month is OCT.
Output:
1-Jul-2024, 31-Jul-2024
1-Aug-2024, 31-Aug-2024
1-Sep-2024, 30-Sep-2024
4. SQL should return previous two months start and end dates when current month is DEC.
Output:
1-Oct-2024, 31-Oct-2024
1-Nov-2024, 30-Nov-2024
5. SQL should return previous Year and month start and end dates when current month is JAN.
Output:
I am not sure is this possible with single SQL query. If Yes please help me with SQL.
Regards
Suji
|
|
|
|
Re: Previous Quarter three months of Start Date and End Dates [message #690127 is a reply to message #690126] |
Wed, 16 October 2024 12:42 |
|
suji6281
Messages: 151 Registered: September 2014
|
Senior Member |
|
|
Hi John,
I have tried with this below SQL.
SELECT to_char(ADD_MONTHS(TRUNC(SYSDATE, 'Q'), -3), 'Q') prev_qtr_no,
ADD_MONTHS(TRUNC(SYSDATE, 'Q'), -3) AS First_Start, last_day(ADD_MONTHS(TRUNC(SYSDATE, 'Q'), -3)) as First_Last,
ADD_MONTHS(TRUNC(SYSDATE, 'Q'), -2) AS Second_Start, last_day(ADD_MONTHS(TRUNC(SYSDATE, 'Q'), -2)) AS Second_Last,
ADD_MONTHS(TRUNC(SYSDATE, 'Q'), -1) AS Third_Start, last_day(ADD_MONTHS(TRUNC(SYSDATE, 'Q'), -1)) AS Third_Last FROM DUAL;
Regards
Suji
|
|
|
|
|
|
Re: Previous Quarter three months of Start Date and End Dates [message #690131 is a reply to message #690130] |
Thu, 17 October 2024 06:31 |
cookiemonster
Messages: 13952 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
And given that in your original examples the number of rows returned differed in different months, saying it will be fine with 2 columns or 7 is fairly unhelpful. For example - for JAN, which of the 7 columns should be populated, given your example output only has 2 values?
Just tell us what output you want, and what logic should be used to determine that output and we can give you the SQL to do it.
|
|
|