Home » SQL & PL/SQL » SQL & PL/SQL » Joining Two View (Oracle 11G)
Joining Two View [message #640537] Thu, 30 July 2015 08:49 Go to next message
samyms
Messages: 18
Registered: May 2015
Location: Chennai
Junior Member
Hi

I have problem when joining two 2 views.

ASP_RP_SP_V is view which is doing product value calculation.
when we pass the value directly to this view. it resulting properly (like below)

Example: (working fine)
select * from ASP_RP_SP_V t
where t.lbl='00123' and
t.prod = '0111' and
t.pckg = '01' and
t.id = 140

select * from INP_SP_V --- Only one row its having
output:
lbl prod pckg id
00123 0111 01 140

When i join with other view(INP_SP_V ), then its throwing divide by zero error. but if we pass the value directly then its showing the proper result.

select * from ASP_RP_SP_V t1 , INP_SP_V t2
Where t1.lbl=t2.lbl
and t1.prod=t2.prod
and t1.pckg=t2.pckg
and t1.id=t2.id

Can you please help me how oracle joining these views in background and throwing the error.

Re: Joining Two View [message #640538 is a reply to message #640537] Thu, 30 July 2015 08:54 Go to previous messageGo to next message
John Watson
Messages: 8931
Registered: January 2010
Location: Global Village
Senior Member
Please read our OraFAQ Forum Guide and How to use [code] tags and make your code easier to read
You have been aske to do this before.

Then, you need to show what you are doing: the queries and the results, with copy/paste enclosed within [code] tags. The source code for the views would help, too.
Re: Joining Two View [message #640539 is a reply to message #640537] Thu, 30 July 2015 08:55 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
http://www.orafaq.com/forum/mv/msg/197301/637082/#msg_637082
Re: Joining Two View [message #640542 is a reply to message #640539] Thu, 30 July 2015 09:17 Go to previous messageGo to next message
samyms
Messages: 18
Registered: May 2015
Location: Chennai
Junior Member
Hi
The ASP_RP_SP_V t has more lines and its has confidential logic. that why i didn't share.
But they are diving the values in many places.
Re: Joining Two View [message #640543 is a reply to message #640542] Thu, 30 July 2015 09:22 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
samyms wrote on Thu, 30 July 2015 07:17
Hi
The ASP_RP_SP_V t has more lines and its has confidential logic. that why i didn't share.
But they are diving the values in many places.


so don't divide by ZERO & error won't get thrown.

Your code results in the error so a code change is required to prevent the error.

What more can we say?
Re: Joining Two View [message #640544 is a reply to message #640543] Thu, 30 July 2015 09:25 Go to previous messageGo to next message
Bill B
Messages: 1971
Registered: December 2004
Senior Member
Instead of having the code

select col1/col2 from mytable

use

select decode(col2,0,0,col1/col2) from mytable;
Re: Joining Two View [message #640545 is a reply to message #640543] Thu, 30 July 2015 09:30 Go to previous messageGo to next message
samyms
Messages: 18
Registered: May 2015
Location: Chennai
Junior Member
When we run the ASP_RP_SP_V with value (which we got from INP_SP_V) in where condition , its working .. but why its not working when combining with INP_SP_V view( its having only one row)

select * from ASP_RP_SP_V t
where t.lbl='00123' and
t.prod = '0111' and
t.pckg = '01' and
t.id = 140
Re: Joining Two View [message #640546 is a reply to message #640544] Thu, 30 July 2015 09:30 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
add to WHERE clause
AND divisor != 0
Re: Joining Two View [message #640547 is a reply to message #640546] Thu, 30 July 2015 09:35 Go to previous messageGo to next message
samyms
Messages: 18
Registered: May 2015
Location: Chennai
Junior Member
I didn't understand How its working when we pass the value directly into where condition.
Re: Joining Two View [message #640548 is a reply to message #640547] Thu, 30 July 2015 09:36 Go to previous messageGo to next message
samyms
Messages: 18
Registered: May 2015
Location: Chennai
Junior Member
The same view , is working when we pass the value into where condition. but its not working having the same value in other view while joining it.
Re: Joining Two View [message #640549 is a reply to message #640548] Thu, 30 July 2015 09:41 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
We can't answer your question since you refuse to post code so we can reproduce what you report.

By definition, they are NOT the same.
You need to choose one of two options.
1) fix the code
2) live with what you have.
Re: Joining Two View [message #640550 is a reply to message #640549] Thu, 30 July 2015 09:43 Go to previous messageGo to next message
samyms
Messages: 18
Registered: May 2015
Location: Chennai
Junior Member
Thank you for your help. I will try and let you know if find anything.

Note: Don't mistake me. AS the code is SOX compliant and i should not share.
Re: Joining Two View [message #640551 is a reply to message #640550] Thu, 30 July 2015 09:48 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
BTW, nobody here would know or care if any posted code is SOX compliant or not.
How ridiculous.
Re: Joining Two View [message #640552 is a reply to message #640551] Thu, 30 July 2015 09:52 Go to previous messageGo to next message
samyms
Messages: 18
Registered: May 2015
Location: Chennai
Junior Member
you thing its silly, but when organization found their code outside(in google).. Then its big problem
Re: Joining Two View [message #640553 is a reply to message #640552] Thu, 30 July 2015 09:57 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

In this case, don't post your questions on the web and hire a consultant to confidentially help you.
Many of us here will do it under a contract which will guarantee the confidentiality, you can contact them through PM.

Re: Joining Two View [message #640554 is a reply to message #640553] Thu, 30 July 2015 10:07 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
If the division is in the where clause then a change in access path can cause it in some cases and not others.
Say you're looking for rows where col1 is A and rows where col1 is not A have the divisor column set to 0.
An index scan of an index on col1 will avoid the problem data but a full table scan will pick it up.

Bill B's suggestion will avoid the error.
Re: Joining Two View [message #640555 is a reply to message #640554] Thu, 30 July 2015 10:20 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Depending on the case, you can also use the NO_MERGE hint:

select /*+ no_merge(t1) no_merge(t2) */ * from ASP_RP_SP_V t1 , INP_SP_V t2...


Or in the opposite force the merge:

select /*+ merge(t2) */ * from ASP_RP_SP_V t1 , INP_SP_V t2...


Or materialize one view:

with v as (select lbl, prod, pckg, id from INP_SP_V where ... and rownum > 0),
select * from ASP_RP_SP_V t1 , v t2 where ...


Or rewrite it with a subquery:

select * from ASP_RP_SP_V t1 where (lbl, prod, pckg, id) in (select lbl, prod, pckg, id from ... )


Or...

--moderator edit: added code tags Smile

[Updated on: Thu, 30 July 2015 11:22] by Moderator

Report message to a moderator

Re: Joining Two View [message #640556 is a reply to message #640555] Thu, 30 July 2015 10:27 Go to previous messageGo to next message
Bill B
Messages: 1971
Registered: December 2004
Senior Member
Michel,
Really nice write up of the available options. Your the man!!
Re: Joining Two View [message #640589 is a reply to message #640556] Fri, 31 July 2015 06:40 Go to previous messageGo to next message
samyms
Messages: 18
Registered: May 2015
Location: Chennai
Junior Member
When i selecting some thing instead of * then its working. Thank you...
select t1.prod,t1.pckg from ASP_RP_SP_V t1 , INP_SP_V t2
Where t1.lbl=t2.lbl
and t1.prod=t2.prod
and t1.pckg=t2.pckg
and t1.id=t2.id


Re: Joining Two View [message #640593 is a reply to message #640589] Fri, 31 July 2015 07:15 Go to previous message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
1) You should always select only the columns you need.
2) Reducing the select list increases the chances of oracle using index scans rather than full table scans. So the problem is probably what I said above. What you've done can not be considered a permanent fix - a change in stats can cause a change in the plan and make the error reappear. To be sure you need you fix the division code in the view as Bill B suggested above
Previous Topic: Union Join
Next Topic: problem in record type
Goto Forum:
  


Current Time: Thu Apr 25 00:47:23 CDT 2024