Home » SQL & PL/SQL » SQL & PL/SQL » SQL date overlapping
| SQL date overlapping [message #634975] |
Wed, 18 March 2015 09:17  |
 |
sanodani
Messages: 98 Registered: October 2014
|
Member |
|
|
Hallo all..
I am trying to check date overlapping in SQL query, but i dont know, how can i do this.. can anyone please help me in this regards?
my case.. :
create table testpro (
pid number,
group_nr_from number,
f_date date,
group_nr_till number,
t_date date
);
insert into testpro (pid, group_nr_from, f_date, group_nr_till, t_date) values (123, 0, '01.02.2008',150, '21.12.2008');
insert into testpro (pid, group_nr_from, f_date, group_nr_till, t_date) values (120, 151, '01.02.2008',200, '21.12.2008');
insert into testpro (pid, group_nr_from, f_date, group_nr_till, t_date) values (125, 201, '01.02.2008',300, '21.12.2008');
insert into testpro (pid, group_nr_from, f_date, group_nr_till, t_date) values (127, 250, '01.02.2008',500, '21.12.2008');
insert into testpro (pid, group_nr_from, f_date, group_nr_till, t_date) values (130, 300, '01.02.2008', 500, '21.12.2008');
Hoping for your Kind cooperation.
thankx in advance
regards,
|
|
|
|
| Re: SQL date overlapping [message #634977 is a reply to message #634975] |
Wed, 18 March 2015 09:22   |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
What is the desired output? What are the rules to derive at the desired output? And, what did you try so far? Please post the required details.
Seems like you need to use LAG function.
|
|
|
|
|
|
|
|
|
|
| Re: SQL date overlapping [message #635016 is a reply to message #635005] |
Thu, 19 March 2015 02:27   |
 |
sanodani
Messages: 98 Registered: October 2014
|
Member |
|
|
Hallo all,
Thank you very much for you Input, and Suggestion/Feedback.
Well, I am trying to find/check if there is any Group that they are overlapping the same date time.
like in my case example,
Group_nr_from: 250 and Group_nr_till: 500 is overlapping the above
Group_nr_from: 201 and Group_nr_till: 300 because the Groups 250 is already inside the 300
so i am trying to check if there any Group making this overlapping or not, if it is, then should Show which Group_nr_from/till.
I hope i make it bit clear.. 
Ihave tried this query but did not get the result what i wanted to have.. :/
select a.group_nr_from, a.group_nr_till from testpro a where exists (select 1 from testpro b where b.f_date <= a.t_date and b.t_date >= a.f_date) order by 2,1;
thankx again
regards,
|
|
|
|
|
|
| Re: SQL date overlapping [message #635018 is a reply to message #635017] |
Thu, 19 March 2015 03:20   |
 |
sanodani
Messages: 98 Registered: October 2014
|
Member |
|
|
Hallo Michael
Thankx for your prompt reply.. 
well, 'Group_nr_from" and "Group_nr_till" Play role and also the "PID". The _from and _till Group nr is the Group that there lies different other groups within that range.
I have to check if there is any PID, that are in the same Group range, are overlapping the date?
if it is so, then should Show Output, which pid, Group_nr_from and _till are overlapping?
i have changed bit my insert query with one new insert Statement with the same date value.
insert into testpro (pid, group_nr_from, f_date, group_nr_till, t_date) values (127, 250, '01.02.2008',500, '21.12.2008');
I am sorry, its bit hard to explain and sorry to bother you...
regards,
|
|
|
|
|
|
|
|
|
|
| Re: SQL date overlapping [message #635022 is a reply to message #635021] |
Thu, 19 March 2015 04:42   |
 |
sanodani
Messages: 98 Registered: October 2014
|
Member |
|
|
sry for that...
here is my insert Statement ..
insert into testpro (pid, group_nr_from, f_date, group_nr_till, t_date) values (123, 0, '01.02.2008',150, '21.12.2008');
insert into testpro (pid, group_nr_from, f_date, group_nr_till, t_date) values (120, 151, '01.02.2008',200, '21.12.2008');
insert into testpro (pid, group_nr_from, f_date, group_nr_till, t_date) values (125, 201, '01.02.2008',300, '21.12.2008');
insert into testpro (pid, group_nr_from, f_date, group_nr_till, t_date) values (127, 250, '01.02.2008',500, '21.12.2008');
insert into testpro (pid, group_nr_from, f_date, group_nr_till, t_date) values (130, 300, '01.02.2008', 500, '21.12.2008');
insert into testpro (pid, group_nr_from, f_date, group_nr_till, t_date) values (127, 250, '01.06.2008',500, '21.12.2008');
commit;
and the current Output is :
PID Group_NR_FROM F_DATE GROUP_NR_TILL T_DATE
120 151 01.02.2008 200 21.12.2008
123 0 01.02.2008 150 21.12.2008
125 201 01.02.2008 300 21.12.2008
127 250 01.06.2008 500 21.12.2008
127 250 01.02.2008 500 21.12.2008
127 250 01.02.2008 500 21.12.2008
130 300 01.02.2008 500 21.12.2008
thank you.
regards,
|
|
|
|
|
|
| Re: SQL date overlapping [message #635035 is a reply to message #635029] |
Thu, 19 March 2015 06:55   |
 |
sanodani
Messages: 98 Registered: October 2014
|
Member |
|
|
Hallo Michel
here is my insert query and its Output and my required Output(but its not the correct, what i want to have..)
insert into testpro (pid, group_nr_from, f_date, group_nr_till, t_date) values (123, 0, '01.02.2008',150, '21.12.2008');
insert into testpro (pid, group_nr_from, f_date, group_nr_till, t_date) values (120, 151, '01.02.2008',200, '21.12.2008');
insert into testpro (pid, group_nr_from, f_date, group_nr_till, t_date) values (125, 201, '01.02.2008',300, '21.12.2008');
insert into testpro (pid, group_nr_from, f_date, group_nr_till, t_date) values (127, 250, '01.02.2008',500, '21.12.2008');
insert into testpro (pid, group_nr_from, f_date, group_nr_till, t_date) values (130, 300, '01.02.2008', 500, '21.12.2008');
insert into testpro (pid, group_nr_from, f_date, group_nr_till, t_date) values (127, 260, '01.06.2008',300, '21.12.2008');
commit;
PID Group_nr_From F_date Group_nr_till T_date
127 260 01.06.2008 300 21.12.2008
123 0 01.02.2008 150 21.12.2008
120 151 01.02.2008 200 21.12.2008
125 201 01.02.2008 300 21.12.2008
127 250 01.02.2008 500 21.12.2008
130 300 01.02.2008 500 21.12.2008
and here is my result Output, what i would like to have, but its not the correct, because i get the repeated pid which have the same pid and the group already resides another group range and date is overlapping :/
select a.pid, a.group_nr_from, a.group_nr_till from testpro a where exists (select 1 from testpro b where b.f_date <= a.t_date and b.t_date >= a.f_date) order by 2,1;
PID Group_Nf-From Group_Nr_Till
123 0 150
120 151 200
125 201 300
127 250 500
127 250 500
127 250 500
130 300 500
where i would like to avoid such overlapping data.
thank you
regards
|
|
|
|
|
|
|
|
|
|
Goto Forum:
Current Time: Thu Aug 27 02:15:25 CDT 2026
|