SQL Full Outer Join and combining two columns - sql

I have the following two tables:
TableA:
UserID PermissionTableA
UserA PermissionA
UserB PermissionA
UserC PermissionB
UserD PermissionB
TableB:
UserID PermissionTableB
UserA PermissionC
UserA PermissionD
UserB PermissionC
UserB PermissionD
The two tables shall be joined via ID and the Permissions should be combined into one single column, like this:
Solution:
UserID Permission
UserA PermissionC
UserA PermissionD
UserA PermissionA
UserB PermissionC
UserB PermissionD
UserB PermissionA
My current solution however, with full outer join, looks like this:
UserID PermissionTableA PermissionTableB
UserA PermissionC -
UserA PermissionD -
UserA - PermissionA
UserB PermissionC -
UserB PermissionD -
UserB - PermissionA
Is it possible to combine both columns into one, similar to the solution table above with a SQL query (using MS Access).
Thanks in advance.

You are not looking for a join :-) A join combines records. What you are after is the union of two query results.
select UserID, PermissionTableA as Permission
from TableA
UNION
select UserID, PermissionTableB
from TableB
where UserID in (select UserID from TableA)
order by UserID, Permission;
UPDATE: You may as well use your outer join with NZ. Sorry, it took me some time to see what the query was supposed to do.
select UserId, NZ(PermissionTableA, PermissionTableB) as Permission
from ...

Related

Access: Query specific users not in a profile

I have a table that displays user profile and users assigned.
Example
Profile User
-------- -----
ProfileA UserA
ProfileA UserB
ProfileA UserC
ProfileB UserA
ProfileB UserD
ProfileC UserB
ProfileD UserE
I am trying to create a query to verify that a group of specific users are not in the profile and display those profiles.
Example:
I want to display the profiles where UserA and UserC are not assigned to.
Output Example:
Profile
--------
ProfileC
ProfileD
I am trying to create this query in MS Access.
Presumably, you have a table of profiles. If so, use not exists:
select p.*
from profiles as p
where not exists (select 1
from user_profiles up
where up.profile = p.profile and up.user in ('UserA', 'UserC')
);
If you don't have a separate table -- which would seem odd to me -- you can do this with the profiles in your table. But in this case, conditional aggregation is a simple method:
select profile
from user_profiles
group by profile
having sum(iif(user in ('UserA', 'UserC'), 1, 0)) = 0;

SQL extract unique data from two columns in same table

I have a table friends, which contains the following columns:
friend_id and friend_of (both are storing unique user ids)
lets say the table friends contains the following data:
friend_id | friend_of
-------------------------
123 | 456
456 | 789
456 | 123
So this means that:
user with id=123 have one friend with id=456
user with id=456 have two friends with ids=123 (friend_1) & 789(friend_2)
user with id=789 have one friwnd with id=456
I want to write a query that given a single user id shows every friend that this user has (with their ids).
For example:
if given user with id=123 the output would be users with ids=456
if given user with id=789 the output would be users with ids=456
if given user with id=456 the output would be users with ids=123 and 789
Can you help me with the query I need?
(select friend_id as all_friends from friends where friend_of=ID)
uninon
(select friend_of as all_friends from friends where friend_id=ID)
I suppose you are interested in the case where an id exists only in one of the columns. Above query would address this. Note that union is used here and not union all as unique values are required.
select friend_id, friend_of
where friend_id = '456'
just change ID to get desire ouput
Just use union
Declare #id int = 1;
select f.friendof from
#YourTableName as f where f.friendId = #id
union
select f.friendId from
#YourTableName as f where f.friendof = #id
You can use the query SELECT * FROM friends WHERE friend_id='456', which should get all of the friends of 456. Then do a join on your "users" table using the foreign key friend_of.
EDIT: I didn't realize friends was a two-way relationship. In that case, use a UNION first, some of the other responses talk about it. :)

Count rows of right table when joinning two tables using SQL Query

I have a question about SQL Query. Let me do the example to illustration my issue:
I have two tables like this:
Roles Table
ID Role Role Description
1 Administrator Someone in administrator board
2 User Someone who has an account
3 Guess Someone who just view the website
Users Table
ID Username RoleID
1 trind08 1
2 trind09 1
3 trind10 1
4 kimchi 2
5 linhchi 2
6 thanh01 2
7 thanh02 3
8 kiemanh 3
9 liemanh 3
My issue is I want to view all roles and count the user who resolve to them.
Result table after running the query might look like this:
ID Role Role Description Cound of User
1 Administrator Someone in administrator board 3
2 User Someone who has an account 3
3 Guess Someone who just view the website 3
My first try to create a SQL Query like this:
select rol.*, usrCout as 'Count of User' from Roles rol
left join (select count(*) from Users where RoleID == rol.ID) usrCout;
But my query run unsuccessfully and I can't get the result I want. Please help me for this.
Thank you
SELECT
ID
,ROLE
,Role Description
,(SELECT COUNT(*) FROM Users where RoleID = rol.ID) AS UserCount
FROM Roles rol
Try this query
Select count(*) as 'Count of User', r.RoleID, Role, Role Description from role r, Users u where u.RoleId = r.Id group by r.RoleID,Role, Role Description;
Fiddle
Hope this helps

Sql Server 2008 With Array

I have two tables in my sql:
Users :
id name roleid
1 David 1
2 Sean 2
3 Joe 1
Roles:
roleid desc
1 copy
2 delete
3 move
Now i use this cmd to select the user with the user permission
SELECT * FROM Users u INNER JOIN Roles r ON u.roleid = r.roleid
Now i want to know if it's possible to build SQL Table(Roles Table), that it's will be dynamically the number of roleid for each user. something like:
Users :
id name roleid roleid2 roleid3
1 David 1 2 3
2 Sean 2
3 Joe 1 3
Use an associative entity to address the many-to-many relationship between Users and Roles. A composite primary key in the UserRole table will prevent duplicate assignment of roles, and foreign keys referencing the Users and Roles table will preserve referential integrity.
See SQL fiddle for a sample implementation.
At first, I suggest you to use many to many relationship. It means trird table: UsersRoles (userid,roleid)
At second, it's impossible create dynamic number of columns in typical SQL statement. But its possible by using stored procedures.
here is working example
sqlfiddle

Find all user in table A that are also in Table B

I have table A with fields user1 and user2 and table B with user3 and user4 pairs.
I want to be able to find any rows from table A where the user1/user2 combination is also in table B.
The order doesn't matter. For example table A with user1=Mike, user2=Joe would match table B with user3=Joe and user4=Mike.
I'd probably just use an explicit or in a join:
select user1, user2
from tableA join tableB on
(user1=user3 and user2=user4) or
(user1=user4 and user2=user3)
...but take it with a grain of salt. I've been accused of over-using joins, probably with at least a little reason.
select a.user1, a.user2 from a, b
where (a.user1 == b.user3 and a.user2 == b.user4)
or (a.user1 = b.user4 and a.user2 = b.user3);