Join two tables and if the value on the first table is null, but second table has values, take the values of the second table

The name of the picture


Join two tables and if the value on the first table is null, but second table has values, take the values of the second table



I have users and groups. Each of them can be given permission to do something on tables (for example "SELECT", "INSERT",..). Now I want to have a table with usernames and their permissions.



So if a user has no specific permission to SELECT form tableA, but he is in the group IT, which has permission to SELECT from that tableA, his permission on tableA should be SELECT instead of NULL.


SELECT


tableA


SELECT


tableA


SELECT


NULL



A user should be able to have multiple rights.



If a user has at least one right (no matter if specific given to user or group) it should display all rights



If a user has no rights, it should display NULL



Here are my example tables:


MEWOSTFL


select/insert


MEWOGEIM


IT


insert


MEWOWAD


IT


select


MEWOOZF


IT


MEWOBIA


IT


IT


select



table1



table2



One table with following inputs:


MEWOSTFL


MEWOGEIM


insert/select


MEWOWAD


MEWOOZF


select


MEWOBIA


NULL



UNION both tables already gets me close but there are duplicates.





Edit the question add your expected output also.
– Yogesh Sharma
4 hours ago





@YogeshSharma is that enought or is there anything else you need to know?
– David Walser
4 hours ago






3 Answers
3



You can use coalesce() function :


coalesce()


select coalesce(t1.permission_name, t2.permission_name) as permission_name ,
t1.name
from table2 t2 left join
table1 t1
on t1.name = t2.username;



For your current attempt i didn't find any P alise.


P



EDIT : Perhaps you need FULL OUTER JOIN but your expected output doesn't suggests this.


FULL OUTER JOIN


select coalesce(t1.permission_name, t2.permission_name) as permission_name ,
t1.name
from table1 t1 full outer join
table2 t2
on t1.name = t2.username;



EDIT : I guess you want row_number function :


with t as (
select permission_name, name
from table1
union all
select permission_name, username
from table1
)

select top (1) with ties *
from t
order by row_number() over (partition by name, permission_name order by name);





@Grimm I think you are close to my solution. I edited my question because I forgot something :/
– David Walser
4 hours ago





+1 for coalesce(), but maybe he should do a full join. Thus the query will return values even if t1 is the only table having values for a specific entry (and t2 is lacking this entry).
– Grimm
4 hours ago





@DavidWalser I edited my comment also because a FULL JOIN will be much simpler than a UNION on two LEFT JOINs ;-)
– Grimm
4 hours ago





@DavidWalser ah ok, I see your edit. Then you can simply do a LEFT JOIN (table1 LEFT JOIN table2) and then use COALESCE() as Yogesh Sharma proposed.
– Grimm
4 hours ago





@Grimm what shall I LEFT JOIN (table1 LEFT JOIN table2) with?
– David Walser
4 hours ago



I guess this satisfies your conditions:


SELECT
permission_name
, name
FROM
(
SELECT
coalesce(t2.permission_name, t1.permission_name) as permission_name
, t1.name AS name
FROM
table1
LEFT JOIN table2
ON t1.name = t2.username
) AS _temp
WHERE
NOT _temp.permission_name IS NULL



I assume you don't want rows where t1.permission_name is null and t2.permission_name is also NULL or even t2 doesn't have a row for this entry - hence the outer query (_temp). If you don't need that just remove the outer query entirely.





user can have multiple permissions so unfortunately this is not what I am looking for. I will edit my question
– David Walser
3 hours ago







so the question is edited. It should be very clear now.
– David Walser
2 hours ago





I think you were very close with your original query. I have debugged your code and found that in the case statment you had a few error.



case P.permission_name
when NULL then RolePermissionMember.permission_name
else P.permission_name end as permission_name,
name



should be:



CASE WHEN RolePermissionMember.PermissionName IS NULL THEN
P.PermissionName ELSE RolePermissionMember.PermissionName END as
PermissionName, DName



I have modified it. I also brought in the two columns so you can see the effect.


DECLARE @TABLE1 TABLE(PermissionName varchar(20), DName varchar(20))
INSERT INTO @TABLE1
SELECT 'INSERT', 'MEWOSTFL' UNION ALL
SELECT 'SELECT', 'MEWOSTFL' UNION ALL
SELECT NULL, 'MEWOBIA' UNION ALL
SELECT 'SELECT', 'MEWOWAD' UNION ALL
SELECT NULL, 'MEWOGEIM' UNION ALL
SELECT NULL, 'MEWOOZF'



DECLARE @TABLE2 TABLE(PermissionName varchar(20), UserName varchar(20))
INSERT INTO @TABLE2
SELECT 'SELECT', 'MEWOWAD' UNION ALL
SELECT 'SELECT', 'MEWOGEIM' UNION ALL
SELECT 'SELECT', 'MEWOOZF'




SELECT
RolePermissionMember.PermissionName,
P.PermissionName,
CASE
WHEN RolePermissionMember.PermissionName IS NULL THEN P.PermissionName
ELSE RolePermissionMember.PermissionName END as PermissionName,
DName
FROM @TABLE1 RolePermissionMember
LEFT JOIN @TABLE2 P ON
UserName = DName





I edited my question
– David Walser
2 hours ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Keycloak server returning user_not_found error when user is already imported with LDAP

415 Unsupported Media Type while sending json file over REST Template

PHP parse/syntax errors; and how to solve them?