Home » SQL & PL/SQL » SQL & PL/SQL » finding previous quarter end date
finding previous quarter end date [message #655384] Tue, 30 August 2016 05:13 Go to next message
lappi
Messages: 8
Registered: March 2016
Location: New york
Junior Member
Hi,
I have two input dates as parameters and my query shoud return previous quarter end dates between two dates.
We have four quarters in a year.
Q1 -Jan to March
Q2-Apr to June
Q3-Jul to Sep
Q4-Oct to Dec
Input dates example 'Q1-2015','Q2=2016'

Input Dates
Q1-2015 , Q2-2016

Number of Dates to be returned from query
Dec 31st 2014
March 31st 2015
June 30 2015
Sep 30 2015
Dec 31st 2015
March 31st 2016


Input Dates
Q1-2016 Q2-2016

Number of Dates to be returned from query
Dec 31st 2015
March 31 2016
I need it to be returned in a query.
Thanks for your help.
Re: finding previous quarter end date [message #655387 is a reply to message #655384] Tue, 30 August 2016 05:46 Go to previous messageGo to next message
John Watson
Messages: 8930
Registered: January 2010
Location: Global Village
Senior Member
You should be able to do something with the Q date format specifier. For example,
orclz> select sysdate,to_char(sysdate,'q'),trunc(sysdate,'Q'),trunc(sysdate,'Q')-1 from dual;

SYSDATE    T TRUNC(SYSD TRUNC(SYSD
---------- - ---------- ----------
2016-08-30 3 2016-07-01 2016-06-30

orclz>


--update: expanded the example a bit

[Updated on: Tue, 30 August 2016 05:48]

Report message to a moderator

Re: finding previous quarter end date [message #655390 is a reply to message #655384] Tue, 30 August 2016 06:06 Go to previous messageGo to next message
Michel Cadot
Messages: 68643
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

From your previous topic:

Quote:

Please read and follow the forum guidelines, to enable us to help you:
OraFAQ Forum Guide
How to use {code} tags and make your code easier to read

Please read OraFAQ Forum Guide and How to use [code] tags and make your code easier to read.
Also always post your Oracle version, with 4 decimals, as solution depends on it.

With any SQL or PL/SQL question, please, Post a working Test case: create table (including all constraints) and insert statements along with the result you want with these data then we will be able work with your table and data. Explain with words and sentences the rules that lead to this result.

Also FEEDBACK to your topics, review your previous one, tell how it helps, what is the solution you ended with, thank people who spent time to help you.

[Updated on: Tue, 30 August 2016 06:07]

Report message to a moderator

Re: finding previous quarter end date [message #655397 is a reply to message #655384] Tue, 30 August 2016 07:50 Go to previous messageGo to next message
lappi
Messages: 8
Registered: March 2016
Location: New york
Junior Member
This is required between two dates.
Re: finding previous quarter end date [message #655399 is a reply to message #655397] Tue, 30 August 2016 07:57 Go to previous messageGo to next message
John Watson
Messages: 8930
Registered: January 2010
Location: Global Village
Senior Member
It is required that you write some SQL Smile
Did you try the approach I suggested? I'm not going to write it for you.
Re: finding previous quarter end date [message #655400 is a reply to message #655399] Tue, 30 August 2016 08:48 Go to previous messageGo to next message
lappi
Messages: 8
Registered: March 2016
Location: New york
Junior Member
Hi

I have input only as Q1-2014,no months given, your sql does not give me what I need.
Re: finding previous quarter end date [message #655402 is a reply to message #655400] Tue, 30 August 2016 08:57 Go to previous messageGo to next message
Michel Cadot
Messages: 68643
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Then post a test case.

Re: finding previous quarter end date [message #655403 is a reply to message #655402] Tue, 30 August 2016 09:02 Go to previous messageGo to next message
lappi
Messages: 8
Registered: March 2016
Location: New york
Junior Member
Hi
this is my test case

Input Dates
Q1-2015 , Q2-2016
for above two inputs start date (Q1-2015)and end date(Q2-2016) I need for each quarter between this range previous quarter end date.

Number of Dates to be returned from query
Dec 31st 2014
March 31st 2015
June 30 2015
Sep 30 2015
Dec 31st 2015
March 31st 2016

Thanks for your help in looking.
Re: finding previous quarter end date [message #655404 is a reply to message #655403] Tue, 30 August 2016 09:07 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
Required information

Ensure you include the right information to make your test case reproducible and repeatable. Some suggestions:
SQL and PL/SQL issues or errors:
Include the CREATE table statement, provide sample data (INSERT statements) as well as the expected result. Also include the SQL code that's not working and the error you are getting. For PL/SQL errors, provide the code with the error's line number and the method that was used to call it.
Re: finding previous quarter end date [message #655405 is a reply to message #655404] Tue, 30 August 2016 10:30 Go to previous messageGo to next message
Bill B
Messages: 1971
Registered: December 2004
Senior Member
The easiest way is to setup a date table where the primary key is your quarter code and the other 2 columns show the start and end of the quarter. see below the code to insert into the table. I choose an arbitrary date to start of 01/01/2000. If you need it earlier then simply modify the query.


select 'Q'||to_char(beg_quarter,'q-yyyy') REF_CODE,BEG_QUARTER,END_QUARTER
from
(
    SELECT ADD_MONTHS (DATE '2000-01-01', (LEVEL - 1) * 3)     Beg_quarter,
           ADD_MONTHS (DATE '2000-01-01', (LEVEL - 1) * 3 + 3) - 1 End_quarter
      FROM DUAL
CONNECT BY LEVEL <= 160);

Then you do something as simple as

select beg_quarter,end_quarter
from my_date_table
where ref_code = <input code>;

[Updated on: Tue, 30 August 2016 10:32]

Report message to a moderator

Re: finding previous quarter end date [message #655406 is a reply to message #655403] Tue, 30 August 2016 10:39 Go to previous messageGo to next message
Michel Cadot
Messages: 68643
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
lappi wrote on Tue, 30 August 2016 16:02
Hi
this is my test case
This is NOT a test case and this is NOT formatted
You MUST read the links we have provided you several times and comply.


Re: finding previous quarter end date [message #655418 is a reply to message #655384] Tue, 30 August 2016 18:08 Go to previous messageGo to next message
Barbara Boehmer
Messages: 9090
Registered: November 2002
Location: California, USA
Senior Member
SCOTT@orcl_12.1.0.2.0> variable start_date varchar2(7)
SCOTT@orcl_12.1.0.2.0> variable end_date varchar2(7)
SCOTT@orcl_12.1.0.2.0> exec :start_date := 'Q1-2015'

PL/SQL procedure successfully completed.

SCOTT@orcl_12.1.0.2.0> exec :end_date := 'Q2-2016'

PL/SQL procedure successfully completed.

SCOTT@orcl_12.1.0.2.0> select to_char
  2  	      (add_months
  3  		(to_date
  4  		   (substr (:start_date, 2, 1) * 3 - 2 || '-01-' || substr (:start_date, 4),
  5  		    'mm-dd-yyyy'),
  6  		 (rownum - 1) * 3) - 1,
  7  	       'Mon ddth yyyy') previous_quarter_end
  8  from   dual
  9  connect by level <= 4 - substr (:start_date, 2, 1)
 10  		       + substr (:end_date, 2, 1)
 11  		       + ((substr (:end_date, 4) - substr (:start_date, 4) - 1) * 4)
 12  		       + 1
 13  /

PREVIOUS_QUARTER_END
----------------------
Dec 31st 2014
Mar 31st 2015
Jun 30th 2015
Sep 30th 2015
Dec 31st 2015
Mar 31st 2016

6 rows selected.
Re: finding previous quarter end date [message #655470 is a reply to message #655418] Wed, 31 August 2016 08:18 Go to previous messageGo to next message
lappi
Messages: 8
Registered: March 2016
Location: New york
Junior Member
Barbara

This is what I needed.Thanks for your help.
Re: finding previous quarter end date [message #655471 is a reply to message #655470] Wed, 31 August 2016 08:22 Go to previous message
Michel Cadot
Messages: 68643
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

So what did you learn?

Previous Topic: Help required in SQL Analytic Funciton
Next Topic: QUERY HELP
Goto Forum:
  


Current Time: Tue Apr 23 02:24:07 CDT 2024