join two virtual tables with if-else condition join [message #604556] |
Wed, 01 January 2014 04:01 |
|
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 |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
rc3d wrote on Wed, 01 January 2014 15:31now 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 #604584 is a reply to message #604574] |
Wed, 01 January 2014 11:43 |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
rc3d wrote on Wed, 01 January 2014 20:09thanks 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 #604943 is a reply to message #604938] |
Mon, 06 January 2014 06:51 |
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:22this 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
|
|
|