to derive previous dates and ignore first date [message #641438] |
Fri, 14 August 2015 15:35 |
|
rohit_shinez
Messages: 139 Registered: January 2015
|
Senior Member |
|
|
Hi,
I am having below two tables
periods
START_DATE END_DATE
30-SEP-2005 00.00.00 13-OCT-2005 00.00.00
14-OCT-2005 00.00.00 14-NOV-2005 00.00.00
15-NOV-2005 00.00.00 14-DEC-2005 00.00.00
ref_table
REF_DATE
07-NOV-2005 00.00.00
07-DEC-2005 00.00.00
06-JAN-2006 00.00.00
i need to check ref_date i.e 07-NOV-2005 falling between start and end dates of periods table i.e 14-oct-2005 and 14-Nov-2005(second record) hence derive the previous dates. like this we need to check but the problem here it would for 06-JAN which will not fall in any dates but need to consider the last dates, also to ignore first period dates
output
START_DATE END_DATE ref_DATE
14-OCT-05 14-NOV-05 07-DEC-05
15-NOV-05 14-DEC-05 06-JAN-06
Insert into PERIODS (START_DATE,END_DATE) values (to_date('30-SEP-2005 00.00.00','DD-MON-YYYY HH24.MI.SS'),to_date('13-OCT-2005 00.00.00','DD-MON-YYYY HH24.MI.SS'));
Insert into PERIODS(START_DATE,END_DATE) values (to_date('14-OCT-2005 00.00.00','DD-MON-YYYY HH24.MI.SS'),to_date('14-NOV-2005 00.00.00','DD-MON-YYYY HH24.MI.SS'));
Insert into PERIODS(START_DATE,END_DATE) values (to_date('15-NOV-2005 00.00.00','DD-MON-YYYY HH24.MI.SS'),to_date('14-DEC-2005 00.00.00','DD-MON-YYYY HH24.MI.SS'));
Insert into REF_TABLE (REF_DATE) values (to_date('07-NOV-2005 00.00.00','DD-MON-YYYY HH24.MI.SS'));
Insert into REF_TABLE (REF_DATE) values (to_date('07-DEC-2005 00.00.00','DD-MON-YYYY HH24.MI.SS'));
Insert into REF_TABLE (REF_DATE) values (to_date('06-JAN-2006 00.00.00','DD-MON-YYYY HH24.MI.SS'));
CREATE TABLE "PERIODS"
(
"START_DATE" DATE,
"END_DATE" DATE
);
CREATE TABLE "REF_TABLE"
(
"REF_DATE" DATE
);
|
|
|
Re: to derive previous dates and ignore first date [message #641450 is a reply to message #641438] |
Sat, 15 August 2015 02:18 |
|
Michel Cadot
Messages: 68644 Registered: March 2007 Location: Nanterre, France, http://...
|
Senior Member Account Moderator |
|
|
I don't understand why 07-NOV-2005 which is your example to explain your "specification" is not in the result, otherwise one solution to the sentence but "also to ignore first period dates" which is not understandable, is:
SQL> with maxint as (select max(start_date) max_start, max(end_date) max_end from periods)
2 select nvl(p.start_date, m.max_start) start_date,
3 nvl(p.end_date, m.max_end) end_date,
4 r.ref_date
5 from ref_table r
6 left outer join periods p
7 on p.start_date <= r.ref_date and p.end_date >= r.ref_date
8 left outer join maxint m
9 on m.max_end < r.ref_date
10 order by r.ref_date
11 /
START_DATE END_DATE REF_DATE
----------- ----------- -----------
14-OCT-2005 14-NOV-2005 07-NOV-2005
15-NOV-2005 14-DEC-2005 07-DEC-2005
15-NOV-2005 14-DEC-2005 06-JAN-2006
|
|
|
|
|
|
|
Re: to derive previous dates and ignore first date [message #641457 is a reply to message #641456] |
Sat, 15 August 2015 07:16 |
|
rohit_shinez
Messages: 139 Registered: January 2015
|
Senior Member |
|
|
Ok let me explain you
Start date End date derived_Ref_Date comments
30-Sep-05 13-Oct-05 07-Nov-05 compare 07th nov which falls between start and end dates of periods
table which falls between 14-oct-05
and 14-Nov-05
and map with previous date
14-Oct-05 14-Nov-05 07-Dec-05 compare 07-DEC-2005 which falls between 15-Nov-05
and
14-Dec-05 and map with previous start dates
15-Nov-05 14-Dec-05 06-Jan-06 06-JAN-06 which doesn't falls in any dates needs
to mapped in last periods
After deriving the above then ignore first record
output
START_DATE END_DATE ref_DATE
14-OCT-05 14-NOV-05 07-DEC-05
15-NOV-05 14-DEC-05 06-JAN-06
attached txt file in better format
[Updated on: Sat, 15 August 2015 07:19] Report message to a moderator
|
|
|
|
|
Re: to derive previous dates and ignore first date [message #641461 is a reply to message #641459] |
Sat, 15 August 2015 08:57 |
|
Michel Cadot
Messages: 68644 Registered: March 2007 Location: Nanterre, France, http://...
|
Senior Member Account Moderator |
|
|
Quote:what i mean to say is ref_Date to compare between start and end dates from periods and derive previous dates
You repeat but do not explain anything.
What is the definition of "previous dates"?
Quote:start_DAte end_Date ref_Date
30-SEP-2005 13-OCT-2005 07-NOV-2005
You repeat this all the time, it is not in your result.
Why this one? Where does 30-SEP-2005 come from? Where does come from?
Do NOT refer to the example, tell us WITHOUT REFERRING TO THE EXAMPLE, what result you want.
Say I have 2 tables like yours, I have my data, what should be the result I have to give you to fulfill the requirements?
Say I have 5 periods A, B, C, D, E and 3 dates X, Y, Z what should be the result for Y?
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Re: to derive previous dates and ignore first date [message #641537 is a reply to message #641535] |
Mon, 17 August 2015 10:40 |
|
CraigB
Messages: 386 Registered: August 2014 Location: Utah, USA
|
Senior Member |
|
|
Quote:Insert into PERIODS (START_DATE,END_DATE) values (to_date('30-SEP-2005 00.00.00','DD-MON-YYYY HH24.MI.SS'),to_date('13-OCT-2005 00.00.00','DD-MON-YYYY HH24.MI.SS'));
Insert into PERIODS(START_DATE,END_DATE) values (to_date('14-OCT-2005 00.00.00','DD-MON-YYYY HH24.MI.SS'),to_date('14-NOV-2005 00.00.00','DD-MON-YYYY HH24.MI.SS'));
Insert into PERIODS(START_DATE,END_DATE) values (to_date('15-NOV-2005 00.00.00','DD-MON-YYYY HH24.MI.SS'),to_date('14-DEC-2005 00.00.00','DD-MON-YYYY HH24.MI.SS'));
Insert into REF_TABLE (REF_DATE) values (to_date('07-NOV-2005 00.00.00','DD-MON-YYYY HH24.MI.SS'));
Insert into REF_TABLE (REF_DATE) values (to_date('07-DEC-2005 00.00.00','DD-MON-YYYY HH24.MI.SS'));
Insert into REF_TABLE (REF_DATE) values (to_date('06-JAN-2006 00.00.00','DD-MON-YYYY HH24.MI.SS'));
CREATE TABLE "PERIODS"
(
"START_DATE" DATE,
"END_DATE" DATE
);
CREATE TABLE "REF_TABLE"
(
"REF_DATE" DATE
);
I don't see, in your table definition, how you are relating a record in your REF_TABLE to a record in your PERIODS table. There does not appear to be any Foreign Key defined. Without this, how do you expect to uniquely tie a REF_DATE record to a PERIODS record?
Craig...
|
|
|
|
|
|
|
|
|
Re: to derive previous dates and ignore first date [message #641815 is a reply to message #641806] |
Tue, 25 August 2015 00:09 |
|
Michel Cadot
Messages: 68644 Registered: March 2007 Location: Nanterre, France, http://...
|
Senior Member Account Moderator |
|
|
rohit_shinez wrote on Mon, 24 August 2015 23:44Hi,
I need to refer master_table where i am not seeing your table i mean i need to check the derived max(start_date) start_date, max(end_date) end_date value and compare these dates whether falling between start and end dates of master_table
This is completely meaningless for me.
How this sentence is related to your original question?
How my previous query does not fit your needs?
[Updated on: Tue, 25 August 2015 00:11] Report message to a moderator
|
|
|
|
|
|
|
Re: to derive previous dates and ignore first date [message #641914 is a reply to message #641912] |
Thu, 27 August 2015 08:50 |
cookiemonster
Messages: 13920 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
Well that's because this:
where start_date between ms.start_date and ms.end_date
Is the same as this:
master_table.start_date must always be between master_table.start_Date and anything else.
If you meant to use start_date from a different table you need to use a table name or alias to tell oracle that.
|
|
|
|
|
|
|