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

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

Related

Check condition within table possibly self-join?

I have a users table with many columns however I am trying to use the below 2 columns to fetch the users do not have expired = 1
The query should return only user 2. Any help is much appreciated.
user_id expired
1 1
1 0
2 0
2 0
3 1
I know you have an answer, but if you are just looking for a list of single user_id's this might work a little more efficiently:
select user_id
from users
GROUP BY user_id
having MAX(expired) = 0
You can do this using not in like below :
select * from users where user_id
not in(select distinct user_id from users where expired = 1);
See SQL Here

Rails / ActiveRecord / SQL Return only 1 entry per user_id based on the highest priority

I have slightly complex relation going on my Rails application that I'm having a lot of trouble with.
For starters, there's a table that contains a list of roles which have an id and a priority. An example table looks like this
id priority
1 5
2 4
5 3
6 2
7 1
Now there's also a join_table that likes user ids with role ids in order to keep track of which user is assigned which role. The complicated part is that a user can have many roles
user_id role_id
3 1
3 2
3 7
4 1
4 5
4 6
What I'm trying to get is to filter done the users_roles table to only 1 entry per user based on the role with the lowest priority.
So what I'm trying to end up with is:
user_id role_id
3 7
4 6
The reason I would end up with this is due to the fact that roles 7 and 6 have the lowest priority.
Any help would be much appreciated
I'm pretty sure I might have solved this my self. The query I'm using is:
SELECT user_id, role_id, min(roles.priority)
FROM users_roles INNER JOIN roles
ON users_roles.role_id = roles.id
GROUP BY user_id

Compare bitmasks in the H2 database

Is there a way to compare bitmasks in the h2 database, similar to what has been asked in Comparing two bitmasks in SQL to see if any of the bits match ?
Having a table of users with different roles, I'd like to select all users that are programmers.
User Table
----------
ID Username Roles
1 Dave 6
2 Charlie 2
3 Susan 4
4 Nick 1
Roles Table
-----------
ID Role
1 Admin
2 Programmer
4 Designer
The select should be something like
SELECT * FROM UserTable WHERE Roles & 2 != 0
I know there is a BIT_AND function in h2 but do not know how to use it.
I was confused by the BITAND and BIT_AND functions. The select statement should look like
SELECT * FROM UserTable WHERE BITAND(Roles, 2) != 0

Select Count from One to Many Table

I am using MS SQL.
I have a access group/permission one to many table setup for my security system. Users are assigned a Group. Each group can have a number of various permissions. I need a select statement that checks (returns a count) if a group has a specific permission.
The data I will have for the select statement is the GroupID of the User and the permissionName they are requesting access to.
Logically what I need is:
Get the ID of the permissionName. pID = SELECT permissionID FROM Permission_List WHERE permissionName = 'News'
Check Group_Permission Table to see if GroupID of the User intersects with permissionID equaling pID
If the count is 0 the group doesn't have access. If the count is 1 the group has access.(I'll handle this in PHP)
TABLE: Group_List
groupID groupName groupDesc
1 Standard Normal Access Level.
2 Limited Limited Access Level.
3 Medium Medium Access Level.
TABLE: Permission_List
permissionID permissionName permissionProtect permissionDesc
1 News 0 News section access.
2 Forums 0 Forums section access.
3 Contacts 0 Contact section access.
TABLE: Group_Permission
groupID permissionID
1 1
1 2
1 3
2 1
3 1
3 2
You should be able to JOIN the tables together and use the COUNT aggregate. For this example, I've assumed you are supplying a group id and a permission name.
SELECT COUNT(*)
FROM Group_Permission GP
JOIN Permission_List PL ON GP.permissionId = PL.permissionId
WHERE PL.permissionName = 'News'
AND GP.groupId = #someGroupId
If count is greater than 0, then the permission exists for that group.

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