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;
Related
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. :)
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 ...
My question revolves around using an Oracle Database to manage a mapping between Raw Entitlements to Business Friendly Roles.
Basically, I have two tables:
Mapping Table - this would contain what entitlements are required to fit into a particular applicationrole. Note that you must have ALL of the entitlements for a particular applicationrole to have it. Also, this could change on any day, so queries need to be dynamic in the sense that it could be 3 entitlements = a role or 10 entitlements = a role.
Application ApplicationRole Resource Action
--------------------------------------------------------
Test1 Admin appserver1 admin
Test1 Admin appserver2 admin
Test1 Admin appserver3 admin
test2 ReadOnly appserver1 ro
test2 ReadOnly appserver2 ro
Accounts Table - this table would contain raw data from servers, like what accounts exist on what servers:
Account Resource Action Application
-------------------------------------------------
abc123 appserver1 admin Test1
abc123 appserver2 admin Test1
abc123 appserver3 admin Test1
test2 ReadOnly appserver1 ro
What I am aiming for is to find what applicationroles (business friendly grouping) are applicable to my accounts. In this example, account abc123 has 3 entitlements, for appservers 1, 2 and 3, and has the admin entitlement. Looking at the mapping table, I can now say this account has applicationrole "admin". However, account test2 only has ro on a single server, and the mapping says it needs ro on two servers to have the role "ReadOnly", therefore, account test2 does NOT have the role.
The output from a query on this same data should look like:
Account Application ApplicationRole
----------------------------------------------
abc123 Test1 Admin
Later on, I'll also want a query that returns the opposite;all accounts that DON'T fit into a role. E.g.
Account Application Resource Action
----------------------------------------------
test2 test2 ReadOnly appserver1
Let me know if I can provide any more info! I can't really find what I am after online, seems pretty hard to search for.
Thanks guys! :)
EDIT:
I've managed to write up this query and it seems to work for the first part; not sure if it's the best way though, and any guidance would be great :)
SELECT *
FROM TEMP_USERDATA b
LEFT JOIN TEMP_MAPPINGTABLE a
ON a.application = b.application
AND a.oresource =b.oresource
AND a.action =b.action
WHERE (SELECT COUNT(c.application||c.oresource||c.action)
FROM temp_mappingtable c
WHERE c.application=a.application) =
(SELECT COUNT(DISTINCT application||oresource||action||account)
FROM temp_userdata
WHERE temp_userdata.application=a.application
);
Try this:
;WITH mapingdata AS ( SELECT application,
applicationrole,
resource,
action,
COUNT ( * ) AS rowcount
FROM temp_mappingtable
GROUP BY application,
applicationrole,
resource,
action),
WITH userdata AS ( SELECT account,
resource,
action,
application,
COUNT ( * ) AS rowcount
FROM user_data
GROUP BY account,
resource,
action,
application)
SELECT *
FROM mapingdata m, userdata u
WHERE m.application = u.application
AND m.resource = u.resource
AND m.action = u.action
AND m.rowcount = u.rowcount;
Have two tables : Applicant , Applies
//APPLICANT
A#
-------------
1
2
3
4
5
//APPLIES
A# POSITION
---------------------
1 GM
2 CEO
3 DIRECTOR
3 MANAGER
So i create the user like this:
CREATE USER TEST IDENTIFIED BY TESTING;
GRANT CREATE SESSION TO TEST;
Now i want grant select to TEST on table APPLICANT which have at least 1 record in APPLIES table using follow query:
SELECT a.A#,COUNT(a.A#) FROM APPLICANT a
INNER JOIN APPLIES ap ON a.A#=ap.A#
HAVING COUNT(a.A#)>0 GROUP BY a.A#;
how i grant to user TEST? with select clause condition
GRANT SELECT ON APPLICANT WHERE (SELECT.......) TO TEST;
Fail to work, errors come out.
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