Home » SQL & PL/SQL » SQL & PL/SQL » Find Out Odd Range (Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production)
Find Out Odd Range [message #645483] Mon, 07 December 2015 08:30 Go to next message
Xandot
Messages: 235
Registered: January 2014
Location: India
Senior Member
Hi All,

Please help me out ..

with t as 
(select 1 ID, 1 Address, 2 Num from dual
union all
select 2 ID, 3 Address, 3 Num from dual
union all
select 3 ID, 7 Address, 6 Num from dual
union all
select 4 ID, 14 Address, 10 Num from dual
union all
select 5 ID, 17 Address, 3 Num from dual)
select * from t 


I want to find out the odd range of address which belongs to other address.

Logic : if Address=14 and Num=10 then 14 to 24 (add+num) is allowed only in row num 4 in address column.

so I want to find out which address's are in not in range or in others range.

For Example: In row number 5 .. address=17 and num=3 but address 17 is occupied by row number 4 so
if I find out is there any other address is present in table which is in between the range of (17-18-19-20) then the output will be like

Output:
ID Address Num
4   14      10



This row will be return because its in range of 17 to 20.

Thanks,
Xandot


Re: Find Out Odd Range [message #645493 is a reply to message #645483] Mon, 07 December 2015 09:11 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

OK what did you try since now?

Re: Find Out Odd Range [message #645494 is a reply to message #645483] Mon, 07 December 2015 09:12 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Quote:
This row will be return because its in range of 17 to 20.


Why the row 4 and not the row 5?

Re: Find Out Odd Range [message #645525 is a reply to message #645494] Tue, 08 December 2015 01:56 Go to previous messageGo to next message
Xandot
Messages: 235
Registered: January 2014
Location: India
Senior Member
Hi Michel,

You can take any row, I just took row=4 because its proper case.

Till now I tried this:

with t as 
(select 1 ID, 1 Address, 2 Num from dual
union all
select 2 ID, 3 Address, 3 Num from dual
union all
select 3 ID, 7 Address, 6 Num from dual
union all
select 4 ID, 14 Address, 10 Num from dual
union all
select 5 ID, 17 Address, 3 Num from dual)
select * from t 
 where address <> 14
   and ((address >= 14 and address <= (14 + 10 ))
    or  ((address + 10) ) >= 14 and
         (address + 10 ) <= (14 + 10 ));


but its returning two rows (Id= 3,5) but it should be return only 5 row. Please help me out .

Thanks
Xandot
Re: Find Out Odd Range [message #645528 is a reply to message #645525] Tue, 08 December 2015 02:24 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

1/ You have to refine your specifications, if you want only one row you need to specify which one.
2/ It returns 3 because 7+10 is greater than 14 and less than 24, last condition in your query.

Re: Find Out Odd Range [message #645535 is a reply to message #645528] Tue, 08 December 2015 03:45 Go to previous messageGo to next message
Xandot
Messages: 235
Registered: January 2014
Location: India
Senior Member
I want to find out the row which under the range of another rows.

Its not mandatory to have only one row, if the number in range of another row then it should be return those rows also.

for example: Address = 14 and Num = 10 then range = 14 to 24 (14+10)

Output should return those rows which fall under the range of 14 to 24.

Is there any other way to check range ?

Thanks
Xandot


Re: Find Out Odd Range [message #645539 is a reply to message #645535] Tue, 08 December 2015 04:05 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Yes, I understand, and I also understand the condition I pointed you to is wrong.
You have to understand why it is true and then fix it.

Re: Find Out Odd Range [message #645578 is a reply to message #645539] Wed, 09 December 2015 01:26 Go to previous messageGo to next message
Xandot
Messages: 235
Registered: January 2014
Location: India
Senior Member
Yes, I understood your point.

I have another example and its working only in one direction :

with t as 
(select 1 ID, 405 Address, 3 Num from dual
union all
select 2 ID, 399 Address, 10 Num from dual)
select * from t 
 where address <> 399
   and ((address >= 399 and address <= (399 + 10 ))
    or  ((address + 3 ) ) >= 399 and
         (address + 3 ) <= (399 + 10 ));


but when I select Address=405 then the condition fails. Can anyone please help me ..how to create generic search ?

Not Working:

with t as 
(select 1 ID, 405 Address, 3 Num from dual
union all
select 2 ID, 399 Address, 10 Num from dual)
select * from t 
 where address <> 405
   and ((address >= 405 and address <= (405 + 3 ))
    or  ((address + 10 ) ) >= 405 and   
         (address + 10 ) <= (405 + 3 ));  -- condition fails (409 <= 407) 



Thanks
Xandot

[Updated on: Wed, 09 December 2015 01:27]

Report message to a moderator

Re: Find Out Odd Range [message #645587 is a reply to message #645578] Wed, 09 December 2015 04:47 Go to previous messageGo to next message
Xandot
Messages: 235
Registered: January 2014
Location: India
Senior Member
Thanks Michel Cadot .. I got the solution. Smile

with t as 
(select 1 ID, 405 Address, 3 Num from dual
union all
select 2 ID, 399 Address, 10 Num from dual
union all
select 4 ID, 396 Address, 5 Num from dual
union all
select 3 ID, 408 Address, 3 Num from dual)
select * from t 
 where address <> 405
   and (
      ((address >= 405 and address <= (405 + 3 ))
    or  address >= 405 and (address + NUM ) <= (405 + 3)
    or  (address + NUM )  >= 405 and (address) <= (405 + 3)
    or  (address + NUM )  >= 405 and (address + NUM) <= (405 + 3))
    ) ;

[Updated on: Wed, 09 December 2015 04:48]

Report message to a moderator

Re: Find Out Odd Range [message #645590 is a reply to message #645587] Wed, 09 December 2015 06:26 Go to previous message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

So now do it in a general way, without constant in the query.

Previous Topic: Help with insert
Next Topic: Comma separated values
Goto Forum:
  


Current Time: Sun Jul 12 13:40:41 CDT 2026