Home » SQL & PL/SQL » SQL & PL/SQL » join two virtual tables with if-else condition join (Oracle 10g)
join two virtual tables with if-else condition join [message #604556] Wed, 01 January 2014 04:01 Go to next message
rc3d
Messages: 213
Registered: September 2013
Location: Baden-Württemberg
Senior Member
Hi

I have two statements

SELECT USER_JC.JC_NAME as Rolle,
       SUBSELECT."Child-Profile" as Subrolle,
       CASE
          WHEN SUBSELECT."Child-Profile" LIKE '%OSADM%' THEN 'OSA'
          WHEN SUBSELECT."Child-Profile" LIKE '%DBA%' THEN 'DBA'
          ELSE 'App'
       END as type,
       PROV_SYS_ENT_USER.FIRSTNAME,
       PROV_SYS_ENT_USER.LASTNAME,
       PROV_SYS_ENT_USER.DEPARTMENT
  FROM USER_JC
       INNER JOIN (SELECT PARENTPROFILE.JC_NAME AS "Parent-Profile",
                          PROFILE.JC_NAME AS "Child-Profile",
                          PROFILE.DESCRIPTION AS "desc"
                     FROM    JOB_CODE PROFILE
                          INNER JOIN
                             JC_JC PARENTPROFILE
                          ON (PARENTPROFILE.CON_JC_NAME = PROFILE.JC_NAME)
                    WHERE PROFILE.JC_NAME LIKE '%LDAP_DIR_33%'
                          AND PROFILE.ADD_INFO_MASTER LIKE '%RoleInRQM=0%'
                   UNION ALL
                   SELECT JC_NAME AS "Parent-Profile",
                          NULL AS "Child-Profile",
                          NULL AS "desc"
                     FROM JOB_CODE
                    WHERE JC_NAME LIKE '%LDAP_DIR_33%'
                          AND ADD_INFO_MASTER LIKE '%RoleInRQM=1%') SUBSELECT
          ON SUBSELECT."Parent-Profile" = USER_JC.JC_NAME
       NATURAL JOIN PROV_SYS_ENT_USER order by USER_JC.JC_NAME


result:

Rolle 					Subrolle 	Type 	Firstname 	Lastname 	Department
UNIX_ADMIN_INSURANCE			                OSA		Hans		Brunner		SAP Basis
TOMCAT_ALL				TOMCAT_WIN	App		Esther		Runner		Middleware
TOMCAT_ALL				TOMCAT_UX	App		Esther		Runner		Middleware


2. statement

SELECT PROFILE_GRP.JC_NAME,
       PROFILE_GRP.UG_NAME,
       SERVERVERBINDUNG.RSS_USER_NAME
  FROM    UG_JC PROFILE_GRP
       INNER JOIN
          RU_UG SERVERVERBINDUNG
       ON PROFILE_GRP.UG_NAME = SERVERVERBINDUNG.UG_NAME
 WHERE     PROFILE_GRP.JC_NAME LIKE '%LDAP_DIR_33%'
       AND PROFILE_GRP.JC_NAME NOT LIKE 'PROV_SYS%'
       AND SERVERVERBINDUNG.RSS_NAME = 'LDAP_DIR_33SRV'


result:

JC_NAME					UG_NAME				RSS_USER_NAME
UNIX_ADMIN_INSURANCE	                UG_UNIX_ADM			host3.orafaq.com	
UNIX_ADMIN_INSURANCE	                UG_UNIX_ADM			host356.orafaq.com	
TOMCAT_WIN				UG_TOMCAT_WIN_ADM 	host2.google.com  
TOMCAT_WIN				UG_TOMCAT_WIN_ADM 	host345.google.com 
TOMCAT_WIN				UG_TOMCAT_WIN_ADM 	host66.google.com  
TOMCAT_UX				UG_TOMCAT_UX_ADM 	host31.google.com  


now I should join those two SQL results together

if Subrolle is NULL
join on Rolle = JC_NAME and don't join on Subrolle

if Subrolle is NOT NULL
join on Subrolle = JC_NAME and don't join on Rolle
Re: join two virtual tables with if-else condition join [message #604558 is a reply to message #604556] Wed, 01 January 2014 04:31 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
rc3d wrote on Wed, 01 January 2014 15:31
now I should join those two SQL results together


So, you have Query_1 and Query_2 result sets which you want to join and get the desired output.

You need to do this :-

Select <Q1.col1,Q1.col2...>,<Q2.col1,Q2.col2...> 
   from (Query_1) Q1, (Query_2) Q2
 where 
   case when <put your conditions here>
        then <result>
   end = <result>
Re: join two virtual tables with if-else condition join [message #604574 is a reply to message #604558] Wed, 01 January 2014 08:39 Go to previous messageGo to next message
rc3d
Messages: 213
Registered: September 2013
Location: Baden-Württemberg
Senior Member
thanks Lalit. I tried, but no success:

 WHERE CASE
 53   54            WHEN Q1.SUBROLLE IS NULL THEN Q1.ROLLE = Q2.JC_NAME
          WHEN Q1.SUBROLLE IS NOT NULL THEN Q1.SUBROLLE = Q2.JC_NAME
 55   56            ELSE END;
          WHEN Q1.SUBROLLE IS NULL THEN Q1.ROLLE = Q2.JC_NAME
                                                 *
ERROR at line 54:
ORA-00905: missing keyword


SQL>



did I understand right? I can't use ANSI JOIN Syntax, I need to use obsolete Oracle style join syntax?
Re: join two virtual tables with if-else condition join [message #604575 is a reply to message #604574] Wed, 01 January 2014 08:48 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
Don't overcomplicate:

select  *
  from  q1,
        q2
  where JC_NAME = NVL(Subrolle,Rolle)
/


SY.

[Updated on: Wed, 01 January 2014 08:49]

Report message to a moderator

Re: join two virtual tables with if-else condition join [message #604584 is a reply to message #604574] Wed, 01 January 2014 11:43 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
rc3d wrote on Wed, 01 January 2014 20:09
thanks Lalit. I tried, but no success:


did I understand right? I can't use ANSI JOIN Syntax, I need to use obsolete Oracle style join syntax?


I agree with Solomon, you are just complicating things. Initially looking at the IF-ELSE conditions, I thought of a CASE or DECODE. NVL of course needs to be there, however, I would like to see a test case to comment further.
Re: join two virtual tables with if-else condition join [message #604589 is a reply to message #604584] Wed, 01 January 2014 12:54 Go to previous messageGo to next message
Littlefoot
Messages: 21808
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
Lalit Kumar B

you are just complicating things

Hm, I'd swear that it was you who suggested that complicated thing /forum/fa/1600/0/
Re: join two virtual tables with if-else condition join [message #604593 is a reply to message #604589] Wed, 01 January 2014 14:35 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Littlefoot wrote on Thu, 02 January 2014 00:24
Lalit Kumar B

you are just complicating things

Hm, I'd swear that it was you who suggested that complicated thing /forum/fa/1600/0/


I thought there are two values "JC_NAME" and "UG_NAME" that needs to be checked against the expression. Now I realize that the value that needs to be checked against the expression is just "JC_NAME". So, NVL would suffice.

It's always good to have more eyes for a better review.
Re: join two virtual tables with if-else condition join [message #604594 is a reply to message #604593] Wed, 01 January 2014 14:43 Go to previous messageGo to next message
Littlefoot
Messages: 21808
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
Certainly.

I just didn't like the way you said it, suggesting that the OP wrote that code "just because". Well, it wasn't "just because" but because you suggested him to do it that way. I think that, after SY posted his opinion, it would be fair (from you) if you said something like "huh, right! I didn't realize that it could be done differently, in a simpler and more efficient manner".
Re: join two virtual tables with if-else condition join [message #604938 is a reply to message #604556] Mon, 06 January 2014 06:22 Go to previous messageGo to next message
rc3d
Messages: 213
Registered: September 2013
Location: Baden-Württemberg
Senior Member
this works:

select  *
  from  q1,
        q2
  where JC_NAME = NVL(Subrolle,Rolle)


but I don't understand why it works Embarassed

is that a cross join?

sorry I am confused, didn't learn this type of query. Please explain me what the code does..
Re: join two virtual tables with if-else condition join [message #604943 is a reply to message #604938] Mon, 06 January 2014 06:51 Go to previous message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
Assuming you understand what nvl does I'm not sure why you can't see that this:
rc3d wrote on Mon, 06 January 2014 12:22
this works:

select  *
  from  q1,
        q2
  where JC_NAME = NVL(Subrolle,Rolle)



Is the simple implementation of this:
rc3d wrote on Wed, 01 January 2014 10:01

if Subrolle is NULL
join on Rolle = JC_NAME and don't join on Subrolle

if Subrolle is NOT NULL
join on Subrolle = JC_NAME and don't join on Rolle



And no it's not a cross join, simply a join that joins one column to one of two others.

[Updated on: Mon, 06 January 2014 06:52]

Report message to a moderator

Previous Topic: Help In optimizing My Query Required Urgently
Next Topic: ORA-00907: missing right parenthesis
Goto Forum:
  


Current Time: Fri Apr 26 12:10:43 CDT 2024