Home » SQL & PL/SQL » SQL & PL/SQL » overlap interval (oracle 9i , window xp)
overlap interval [message #380226] |
Fri, 09 January 2009 11:15 |
Haler
Messages: 71 Registered: October 2004
|
Member |
|
|
Hello,
how to detect if date intervals overlap?
This is the table:
table contrat
(
numcontrat number(5);
datedeb date;
dateend date;
)
example :
numcontrat datedeb dateend
1 02/02/2008 10/03/2008
2 20/03/2008 30/04/2008
3 10/02/2008 15/04/2008
4 16/04/2008 15/05/2008
5 16/05/2008 31/08/2008
6 01/10/2008 31/10/2008
7 10/11/2008 31/12/2008
8 10/11/2008 15/12/2008
numcontrat 3 overlap 1 and 2
numcontrat 4 overlap 2
numcontrat 8 overlap 7
an idea?
|
|
|
|
|
Re: overlap interval [message #380233 is a reply to message #380226] |
Fri, 09 January 2009 11:40 |
flyboy
Messages: 1903 Registered: November 2006
|
Senior Member |
|
|
What about using a simple logic? Two intervals A and B overlap, when A.DATEDEB <= B.DATEEND AND A.DATEEND >= B.DATEDEB, which may be deduced e.g. from a picture: A |---------|
DATEDEB DATEEND
B |-------------------|
DATEDEB DATEEND
Use self join. When 8 overlap 7, then 7 overlap 8 - if you do not include the second detection, add appropriate WHERE condition (A.NUMCONTRAT > B.NUMCONTRAT). In the end, aggregate B interval numbers.
|
|
|
|
|
|
|
Re: overlap interval [message #382012 is a reply to message #382001] |
Tue, 20 January 2009 11:11 |
Haler
Messages: 71 Registered: October 2004
|
Member |
|
|
hello Michel,
I do not understand your request for formatting:
I have attached a formatted text file.
It contains my application and the result I want.
I lined my text by typing, alignment disappears in preview!
Why?
|
|
|
|
Re: overlap interval [message #382087 is a reply to message #381993] |
Tue, 20 January 2009 22:56 |
flyboy
Messages: 1903 Registered: November 2006
|
Senior Member |
|
|
Haler wrote on Fri, 09 January 2009 18:15 | an idea?
|
Haler wrote on Tue, 20 January 2009 16:34 | what is the script that can give me this?
|
I am extremely curious, what do you not understand on the idea I gave you. Or you are not interested in query and need "script" (whatever it means) instead?
If you do not know what self join is (as other parts of the query I already provided), search in SQL Reference book, available with all Oracle documentation e.g. online on http://tahiti.oracle.com/.
By the way, when e.g "numcontrat 8 overlap 7", then also "numcontrat 7 overlap 8". Why did you not specify it too?
|
|
|
|
|
Re: overlap interval [message #382505 is a reply to message #382501] |
Thu, 22 January 2009 15:15 |
|
Michel Cadot
Messages: 68732 Registered: March 2007 Location: Saint-Maur, France, https...
|
Senior Member Account Moderator |
|
|
Quote: | The OP posted more than enough info to answer the question.
|
This is your opinion.
For myself I asked for the result as I didn't understand well.
Flyboy too made appropriate remarks.
Both are not answered.
This is also your opinion and other forum guidelines does not imply this is this forum guidelines.
Quote: | It's about helping others
|
I agree and learning them how to post and helping to express/explain the question is for me the FIRST help I should give as this is the first step to answer a problem.
Not for me, learning is the most important point not getting an answer.
This does not mean that you can't answer but in your side you have not to tell me I can't ask for formatting or more information or detailed explainations.
Quote: | Some hopefully useful links on overlapping dates...
|
Maybe OP didn't want them, maybe he just want a solution.
Anyway we'll never know it as he doesn't give feedback.
I think "Forget this message" means I got the answer.
Regards
Michel
[Updated on: Thu, 22 January 2009 15:17] Report message to a moderator
|
|
|
Re: overlap interval [message #382620 is a reply to message #382505] |
Fri, 23 January 2009 04:16 |
Haler
Messages: 71 Registered: October 2004
|
Member |
|
|
Hello all,
I think the answer is more important than the format
(to a certain point ,I agree).
The problem is that the formatting is lost,
and we spend time to translate into English.
(Yes we can!)
I put two tags (code and /code)!
Thank you anyway for your answers.
|
|
|
Re: overlap interval [message #382636 is a reply to message #382505] |
Fri, 23 January 2009 05:27 |
JRowbottom
Messages: 5933 Registered: June 2006 Location: Sunny North Yorkshire, ho...
|
Senior Member |
|
|
Personally, I feel the forum would be greatly improved if the number of posts that only ask posters to read the guidelines and format their code were much lower.
By all means include these points in a post, but if the above is the only content in a post, I think it is worth considering whether the post actually adds anything to the thread.
People come here to learn, or to get answers to questions that they cannot solve by themselves. They don't come here to be lectured and nagged about their posting style, and if the current levels of nagging continue, I'm sure many of them will simply post elsewhere, and the whole forum will be worse off for this.
|
|
|
Re: overlap interval [message #382750 is a reply to message #382620] |
Sat, 24 January 2009 04:00 |
flyboy
Messages: 1903 Registered: November 2006
|
Senior Member |
|
|
Haler wrote on Fri, 23 January 2009 11:16 | I think the answer is more important than the format
(to a certain point ,I agree).
|
I think that also giving feedback is important too (instead of repeating the same question).
So, did you tried any way posted here or anywhere else? What was the result?
|
|
|
Re: overlap interval [message #383145 is a reply to message #382750] |
Tue, 27 January 2009 07:02 |
Haler
Messages: 71 Registered: October 2004
|
Member |
|
|
Hello,
I think my problem solved this way:
SQL> ed
écrit file afiedt.buf
1 SELECT *
2 FROM CONTRAT A, CONTRAT B
3 WHERE (A.DATEDEB,A.DATEEND) OVERLAPS (B.DATEDEB,B.DATEEND)
4* and a.datedeb<>b.datedeb and a.dateend<>b.dateend
SQL> r
1 SELECT *
2 FROM CONTRAT A, CONTRAT B
3 WHERE (A.DATEDEB,A.DATEEND) OVERLAPS (B.DATEDEB,B.DATEEND)
4* and a.datedeb<>b.datedeb and a.dateend<>b.dateend
NUMSAL NUMCONTRAT DATEDEB DATEEND NUMSAL NUMCONTRAT DATEDEB DATEEND
---------- ---------- -------- -------- ---------- ---------- -------- --------
1 1 02/02/08 10/03/08 1 3 10/02/08 15/04/08
1 2 20/03/08 30/04/08 1 3 10/02/08 15/04/08
1 2 20/03/08 30/04/08 1 4 16/04/08 15/05/08
1 3 10/02/08 15/04/08 1 1 02/02/08 10/03/08
1 3 10/02/08 15/04/08 1 2 20/03/08 30/04/08
1 4 16/04/08 15/05/08 1 2 20/03/08 30/04/08
6 ligne(s) sélectionnée(s).
Script sql for test:
--------------------
create table contrat
(numsal number(4),
numcontrat number(5),
datedeb date,
dateend date
);
insert into contrat values (1,1,'02/02/2008','10/03/2008');
insert into contrat values (1,2,'20/03/2008','30/04/2008');
insert into contrat values (1,3,'10/02/2008','15/04/2008');
insert into contrat values (1,4,'16/04/2008','15/05/2008');
insert into contrat values (1,5,'16/05/2008','31/08/2008');
insert into contrat values (1,6,'01/10/2008','31/10/2008');
insert into contrat values (1,7,'10/11/2008','31/12/2008');
insert into contrat values (1,8,'10/11/2008','15/12/2008');
commit;
it remains to eliminate the symmetry of overlap.
What do you think about this?
|
|
|
Re: overlap interval [message #383167 is a reply to message #383145] |
Tue, 27 January 2009 08:12 |
joy_division
Messages: 4963 Registered: February 2005 Location: East Coast USA
|
Senior Member |
|
|
Haler wrote on Tue, 27 January 2009 08:02 |
3 WHERE (A.DATEDEB,A.DATEEND) OVERLAPS (B.DATEDEB,B.DATEEND)
|
wow, I though I was pretty much up on things, but I never heard of the OVERLAPS function.
Quote: |
insert into contrat values (1,1,'02/02/2008','10/03/2008');
What do you think about this?
|
That part is still bad. You are inserting strings into DATE columns. Where I come from, West of the Atlantic, the second string is October 3rd.
|
|
|
|
Re: overlap interval [message #383171 is a reply to message #380233] |
Tue, 27 January 2009 08:26 |
flyboy
Messages: 1903 Registered: November 2006
|
Senior Member |
|
|
Haler wrote on Tue, 27 January 2009 14:02 | it remains to eliminate the symmetry of overlap.
What do you think about this?
|
flyboy wrote on Fri, 09 January 2009 18:40 | When 8 overlap 7, then 7 overlap 8 - if you do not include the second detection, add appropriate WHERE condition (A.NUMCONTRAT > B.NUMCONTRAT).
|
You were many times asked to specify which of them should be eliminated and which should be left (e.g the one with greater A.NUMCONTRAT should be left). After stating this, it is quite easy to construct the additional WHERE condition, is not it?
Also surprised with OVERLAPS function, as I did not find any remark about it in the documentation. But, the conditions I stated earlier are not more complex; and they let you specify the border conditions (whether e.g. intervals 1-2 and 2-3 overlap or no).
|
|
|
Re: overlap interval [message #383194 is a reply to message #383170] |
Tue, 27 January 2009 12:09 |
joy_division
Messages: 4963 Registered: February 2005 Location: East Coast USA
|
Senior Member |
|
|
Haler wrote on Tue, 27 January 2009 09:21 |
2) for me the format of the date field is good.
|
But you posted a test script for US to test, which will not work for anyone other than yourself (possibly). Why don't you do it correctly and prevent yourself from getting into bad habits early on?
FOO SCOTT>create table contrat
2 (numsal number(4),
3 numcontrat number(5),
4 datedeb date,
5 dateend date
6 )
7 /
Table created.
FOO SCOTT>insert into contrat values (1,2,'20/03/2008','30/04/2008');
insert into contrat values (1,2,'20/03/2008','30/04/2008')
*
ERROR at line 1:
ORA-01843: not a valid month
|
|
|
|
Re: overlap interval [message #385167 is a reply to message #383296] |
Fri, 06 February 2009 15:47 |
ThomasG
Messages: 3212 Registered: April 2005 Location: Heilbronn, Germany
|
Senior Member |
|
|
Quote: |
why no try:
alter session set nls_date_format='DD/MM/YYYY';
|
Because that will in most cases not work in real live.
In real live a user will get the "ORA-01843: not a valid month" error in an application you wrote where he has no chance of altering the session.
That's why implicit conversions are a bad habit and should be avoided in ALL scripts that leak to some public place, like this forum.
|
|
|
Goto Forum:
Current Time: Thu Jan 30 09:50:04 CST 2025
|