I have three tables, Roles, Permissions, and Role_ Permissions
Table Roles has the following: id, role_name
Data: id, role_name
1, Accounting
2, Order_Entry
3, Shipping
Table Permission has: id, permission _name
Data: id, permission _name
1, Admin
2, Super_User
3, Read_Write
4, Read_Only
Table Role_ Permissions has:
role_id (FK from Roles),
permission_id (FK from Permissions)
I need to create a matrix that looks like this:
Heading: Roles Admin Super_User Read_Write Read_Only
Columns/Rows: Accounting X X
Order_Entry X X
Shipping X X
The 'X' represent that there is a permission for that role. Blanks, of course, mean there is no permission for that role
Can this be done in SQL
Related
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;
Currently I have some existing tables about user action and user info and their associated meta info:
action table:
user_id, action_detail
1 "action_click"
2 "action_drag"
user info tablle
user_id, full_name, email
1 "User One" "userone#user.com"
1 "User Two" "usertwo#user2.com"
company info table
company_name, company_domain
"User Company" "user.com"
"User2 Company" "user2.com"
The new requirement I got is:
Building queries that can find all the actions of:
all users from a single company
a single company but exclude certain users specified
multiple companies together but exclude certain users specified
Could anyone give some thoughts about it( especially what is the efficient way to do 2 and 3)?
Requirement #2 is a subset of requirement #3 (a single company is just a list of companies with the size of one). You could use the exists operator to find users under the companies domain, and exclude users based on other conditions:
SELECT *
FROM user u
WHERE full_name NOT IN ('John Doe', 'Jane Doe' /* or any other condition */) AND
EXISTS (SELECT *
FROM company c
WHERE c.company_name NOT IN ('company1', 'company2', /* etc. */) AND
u.email LIKE '%#' || c.company_domain)
EDIT:
To address the conversation in the comments, if you have a large number of ignored users, you may want to have an auxiliary table of ignored users so you can index them and make the search faster.
E.g.:
CREATE TABLE ignored_users (
full_name VARCHAR PRIMARY KEY
);
INSERT INTO ignored_users VALUES ('John Doe');
-- A bunch of other inserts...
SELECT *
FROM user u
WHERE full_name NOT IN (SELECT full_name FROM ignored_users) AND
EXISTS (SELECT *
FROM company c
WHERE c.company_name NOT IN ('company1', 'company2', /* etc. */) AND
u.email LIKE '%#' || c.company_domain)
I need to create a stored procedure to insert data into one table from another based on some conditions. In the existing table, if two columns (primary role and secondary role) have the same value, then I want just one row in the new table with role as primary role.
In case if a row in the old table has different values in primary role and secondary role, I want two rows in the new table, one having the value of role as primary role of old table, and another as secondary.
What is the best way to achieve this?
Right now my query looks something like this
create procedure proc as
begin
insert into newTable values(role)
select primary_role as role from oldTable
where primary_role = secondary_role
end
This does not handle the case where primary role is not the same as secondary role.
Sample
sample row oldTable
PrimaryRole | SecondaryRole | Name
admin | analyst | Sara
sample row newTable
Role | Name
admin | Sara
analyst | Sara
I'm not a sybase expert, but I would do something like this (syntax may need amending). Also, if you are after performance, this can probably be done much more cleverly.
insert into newTable values(role, name) (
select primary_role as role, name from oldTable
where primary_role+name not in (select distinct role+name from newTable)
)
insert into newTable values(role, name) (
select secondary_role as role, name from oldTable
where secondary_role+name not in (select distinct role+name from newTable)
)
This will obviously run 2 different inserts, which is why it could probably be made more performant, but it will essentially try to add all primary roles and all secondary roles, checking to see if the role already exists from the previous run. So no need to check for the case where primary = secondary.
EDIT
Alternatively, you may be able to use UNION ALL:
insert into newTable values(role, name) (
select distinct role, name from (
select primary_role as role, name from oldTable
union all
select secondary_role as role, name from oldTable
)
)
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