sql - Joining tables with different number of rows without duplicates -
i have complicated sql statement creates table of users have rights connect
, appuser
, or both:
(select b.grantee "username", a.granted_role "connect", b.granted_role "appuser" (select grantee, granted_role dba_role_privs granted_role = 'connect') right outer join (select grantee, granted_role dba_role_privs granted_role = 'appuser') b on a.grantee=b.grantee) union (select a.grantee, a.granted_role, b.granted_role (select grantee, granted_role dba_role_privs granted_role = 'connect') left outer join (select grantee, granted_role dba_role_privs granted_role = 'appuser') b on a.grantee=b.grantee)
this produces like:
username connect appuser --------- --------- --------- sue connect appuser bob (null) appuser joe connect (null)
i wish use all_users
table, show users have neither rights. all_users
table shows every user in database.
i have tried adding several types of joins end of sql statement achieve this. closest got adding:
union (select username, null, null all_users)
this produce list each user shown twice, shows users without either right:
username connect appuser --------- --------- --------- amy (null) (null) sue connect appuser sue (null) (null) bob (null) appuser bob (null) (null) joe connect (null) joe (null) (null)
i tried adding where username = a.grantee
, won't work unions. if try replacing union
join
s, adding:
full outer join select username, null, null all_users on username = a.grantee;
i error:
"sql command not ended"
your query seems way more complicated necessary. here 1 approach:
select grantee, max(case when granted_role = 'connect' granted_role end) "connect", max(case when granted_role = 'appuser' granted_role end) "appuser" dba_role_privs group grantee;
if there users have no roles @ all, need all_users
table.
edit:
just use left join
:
select au.username, max(case when granted_role = 'connect' granted_role end) "connect", max(case when granted_role = 'appuser' granted_role end) "appuser" all_users au join dba_role_privs rp on au.username = rp.grantee group au.username;
Comments
Post a Comment