Home » SQL & PL/SQL » SQL & PL/SQL » Need data beween date range (Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production)
Need data beween date range [message #641923] Thu, 27 August 2015 11:22 Go to next message
sathishh.mk@gmail.com
Messages: 3
Registered: August 2015
Junior Member
Hi,

I am new forums, hope i will get some inputs.
I need to show the all data between date range.
Pleae find the attached excel for reference.
Here, the situation i fall into:

Data stored in the table like below:

ID Dept From_date To_date
ID_123 Dep_1 1-Jan-15 31-May-15
ID_123 Dep_2 1-Jun-15 31-Dec-99

I need to show results like below:
ID Dept From_date To_date Comments
ID_123 Dep_1 1-Jan-15 31-May-15
ID_123 Dep_1 1-Feb-15 31-May-15
ID_123 Dep_1 1-Mar-15 31-May-15
ID_123 Dep_1 1-Apr-15 31-May-15
ID_123 Dep_1 1-May-15 31-May-15
ID_123 Dep_2 1-Jun-15 31-Dec-99 Latest Record
ID_123 Dep_2 1-Jul-15 31-Dec-99 Till current Month
ID_123 Dep_2 1-Aug-15 31-Dec-99 Till current Month

Any help would be appriciate.

Thanks,
Sathish
Re: Need data beween date range [message #641924 is a reply to message #641923] Thu, 27 August 2015 11:28 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
Welcome to this forum

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

Re: Need data beween date range [message #641926 is a reply to message #641924] Thu, 27 August 2015 11:32 Go to previous messageGo to next message
manubatham20
Messages: 566
Registered: September 2010
Location: Seattle, WA, USA
Senior Member

What are the rules to get the expected output and what is your expected output?

Manu
Re: Need data beween date range [message #641931 is a reply to message #641923] Thu, 27 August 2015 13:52 Go to previous messageGo to next message
sathishh.mk@gmail.com
Messages: 3
Registered: August 2015
Junior Member
Hi,

we are maintaining ID, Dept data in using From_date, To_date in one of the table same as attached reference file.
But our requirement is to display all months between from_date and to_dates.
Can you please provide any inputs..its really helpful.

Regards,
Sathish
Re: Need data beween date range [message #641933 is a reply to message #641931] Thu, 27 August 2015 13:55 Go to previous messageGo to next message
manubatham20
Messages: 566
Registered: September 2010
Location: Seattle, WA, USA
Senior Member

Inputs are :

Please read rules of forum and provide create table and insert table statements as we don't have time to make create and insert statements and then solve your query.
Also please provide a valid output (how it should look like).

Please read few other posts, how they look like.

Manu
Re: Need data beween date range [message #641935 is a reply to message #641931] Thu, 27 August 2015 14:00 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
sathishh.mk@gmail.com wrote on Thu, 27 August 2015 11:52
Hi,

we are maintaining ID, Dept data in using From_date, To_date in one of the table same as attached reference file.
But our requirement is to display all months between from_date and to_dates.
Can you please provide any inputs..its really helpful.

Regards,
Sathish


http://www.orafaq.com/forum/mv/msg/95011/463394/#msg_463394

post above can be adjusted to meet your specific requirements
Re: Need data beween date range [message #641937 is a reply to message #641931] Thu, 27 August 2015 14:05 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

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.

Before, Please read OraFAQ Forum Guide and How to use [code] tags and make your code easier to read.

Re: Need data beween date range [message #641938 is a reply to message #641933] Thu, 27 August 2015 14:12 Go to previous messageGo to next message
sathishh.mk@gmail.com
Messages: 3
Registered: August 2015
Junior Member
Hi Manu,

Sorry..please find the below statements and do the needful.

drop table test_employee;

CREATE TABLE test_employee
( emp_id varchar2(50) NOT NULL, dept_id varchar2(50) NOT NULL,  emp_from_date date,  emp_to_date date);

INSERT INTO test_employee (emp_id, dept_id, emp_from_date,emp_to_date)
values ('ID_123','Dep_1',to_date('01-01-2015', 'dd-mm-yyyy'),to_date('31-05-2015', 'dd-mm-yyyy'));

INSERT INTO test_employee (emp_id, dept_id, emp_from_date,emp_to_date)
values ('ID_123','Dep_2',to_date('01-06-15', 'dd-mm-yyyy'),to_date('31-12-9999', 'dd-mm-yyyy'));

select * from test_employee;

Data stored in DB like below			
ID	Dept	From_date	To_date
ID_123	Dep_1	1-Jan-15	31-May-15
ID_123	Dep_2	1-Jun-15	31-Dec-99

Required output is:
Need to get results as below				
ID	Dept	From_date	To_date	        Comments
ID_123	Dep_1	1-Jan-15	31-May-15	
ID_123	Dep_1	1-Feb-15	31-May-15	
ID_123	Dep_1	1-Mar-15	31-May-15	
ID_123	Dep_1	1-Apr-15	31-May-15	
ID_123	Dep_1	1-May-15	31-May-15	
ID_123	Dep_2	1-Jun-15	31-Dec-99	Latest Record
ID_123	Dep_2	1-Jul-15	31-Dec-99	Till current Month
ID_123	Dep_2	1-Aug-15	31-Dec-99	Till current Month


Regards,
Sathish


Lalit : Added code tags. It is already been told to you to use code tags.

[Updated on: Fri, 28 August 2015 04:24] by Moderator

Report message to a moderator

Re: Need data beween date range [message #641940 is a reply to message #641938] Thu, 27 August 2015 14:42 Go to previous messageGo to next message
manubatham20
Messages: 566
Registered: September 2010
Location: Seattle, WA, USA
Senior Member

May be something like this
WITH test_employees
        AS (SELECT 'ID_123' id,
                   'Dep_1' dept,
                   TO_DATE ('01-JAN-15', 'DD-MON-YY') from_date,
                   TO_DATE ('31-MAY-15', 'DD-MON-YY') TO_DATE
              FROM DUAL
            UNION ALL
            SELECT 'ID_123' id,
                   'Dep_2' dept,
                   TO_DATE ('01-JUN-15', 'DD-MON-YY') from_date,
                   TO_DATE ('31-DEC-99', 'DD-MON-YY') TO_DATE
              FROM DUAL)
    SELECT id,
           dept,
           ADD_MONTHS (from_date, LEVEL),
           TO_DATE
      FROM test_employees
CONNECT BY LEVEL <= MONTHS_BETWEEN (TO_DATE, from_date);


Manu
Re: Need data beween date range [message #641950 is a reply to message #641938] Fri, 28 August 2015 04:16 Go to previous message
sandeep_orafaq
Messages: 88
Registered: September 2014
Member
What about "Comments"?
Does it come without any rule? Smile
Previous Topic: procedure to give ALTER USER to another
Next Topic: MERGE
Goto Forum:
  


Current Time: Wed Apr 17 20:28:25 CDT 2024