I need to do a SQL Query where I pull users that are in Table 1 and either not in table 2 at all, or not in a specific subset of table 2.
Here is what I have:
Table ClientUsers
ID UserName
1 User1
2 User2
3 User3
4 User4
5 User5
Table UserRoles
ClientUsersID ClientRolesID
1 101
2 166
5 103
I need to select all users that are either in UsersRoles with any ClientRoleID that is not 166 OR not in User Roles at all.
So in this case, I would select users 1, and 3-5. 1 and 5 because they are in table UserRoles with a role other than 166, and 3 and 4 because they are in table ClientUsers but NOT table UserRoles at all.
I haven't been able to use a null in the WHERE statement because then it's looking for a null value in a column, as opposed to a row that doesn't exist.
I think that I have to use a full outer join, then narrow the results, but I haven't found a query that works yet.
SELECT * FROM dbo.ClientUsers
FULL OUTER JOIN dbo.UserRoles on ClientUsers.ID = UserRoles.ClientUsersID
WHERE UserRoles.ClientRolesID != 166 OR [value not in right table]
I don't know how to replace that bracket with something that will work (or redo the query entirely).
Use not exists:
SELECT cu.*
FROM dbo.ClientUsers cu
WHERE NOT EXISTS (SELECT 1
FROM dbo.UserRoles ur
WHERE cu.ID = ur.ClientUsersID AND ur.ClientRolesID = 166
);
This is almost a direct translation of your problem statement.
How about this?
SELECT *
FROM dbo.ClientUsers
FULL OUTER JOIN dbo.UserRoles on ClientUsers.ID = UserRoles.ClientUsersID
WHERE UserRoles.ClientRolesID != 166 or UserRoles.ClientRolesID is NULL;
Related
I have two tables, user:
id
full_name
1
Beatriz
2
Mauro
3
Jose
4
fran
approver :
id
subordinate_id
approver_id
1
1
2
2
3
4
I would like to bring up the names of people who are not registered in the subordinate_id column
I did the following query:
SELECT
U.full_name
FROM user AS U
INNER JOIN approver as A
ON U.id <> A.subordinate_id ;
enter image description here
and still users are coming in that are in the subordinate_id column of the approver table.
I would like to get the result only for user names that are not subordinate_id, can someone help with this?
I would like a result with only the users that are not subordinate_id
This is simple to accomplish an ansi-sql with a not exists semi join:
Select full_name
from user u
where not exists (
select * from approver a
where a.approver_id = u.id <-- or (subordinate_id, whichever it should be)
);
I have two tables (Users and Pairs). The Pairs table contains 3 columns, an ID and then a user1ID and user2ID.
Users
ID firstName surname
------------------------------
1043 john doe
2056 jane doe
Pairs
ID user1ID user2ID
------------------------------
1 1043 2056
I'm then looking at using a select statement to get the user details base on the ID of the Pairs table:
SELECT users1.*, users2.*
FROM Pairs
JOIN Users users1 ON Pairs.user1ID = users1.IDNumber
JOIN Users users2 ON Pairs.user2ID = users2.IDNumber
WHERE Pairs.ID = 1
Which returns the right details for the two users, however they're all on one row, how can I get it to return each user on a separate row as they are in the Users table?
SELECT users1.*, users2.*
FROM Pairs
JOIN Users
ON Pairs.user1ID = users.IDNumber
OR Pairs.user2ID = users.IDNumber
WHERE Pairs.ID = 1
Just use an OR statement in your ON condition instead of 2 joins.
IN will work also.
SELECT *
FROM Pairs p
JOIN Users u ON u.ID IN (p.user1ID, p.User2ID)
WHERE p.ID = 1
Hi friends I struggling with following scenario in SQL table
I have Got Two tables and need a table from querying them.
table 1 is USER table (Master).
USERID USERNAME EMAILADD
113 name1 q#q.com
114 name2 b#bcom
Following is the 2nd Lookup table
userid districtid schoolid schoolNAme radius
113 12332 35831 Fort 1.0
113 2332 35832 Spring 1.0
114 2334 35831 Spring 1.0
and following is my requirement
userid username emailadd schoolid
113 name1 q#q.com 35831
113 name1 q#q.com 35832
114 name2 b#b.com 35831
I tried following query
SELECT userID , userNAME, emailadd, phone,
(SELECT schoolid from Lookup
where UserID IN(select distinct userid from users)) schoolid
FROM Users
but its not working at all.I am new to SQL server can any one please suggest a better way to achieve the result.
Use a join to connect tables
SELECT u.userID, u.userNAME, u.emailadd, u.phone, l.schoolid
FROM Users u
inner join Lookup l on u.userid = l.userid
Use a simple JOIN,
SELECT
Users.userid,username,emailadd,schoolid
FROM
Users
INNER JOIN Lookup ON Users.userid = Lookup .userid
Also you may even want to use "WITH(NOLOCK)" if your only going to select, and if there will be a lot more records you need to iterate through
I have an application where I am showing all complaints to my Logged In agents.
I have 100 of agents who can see the same complaint screen.for example agentA and agentB
can see all the complaints when they logged in.
> Complaint_id Complaint_detail
1 complaint_1
2 complaint_2
3 complaint_3
Now the problem is I have to add functionality that every agent can put comments for their ease or you can say a reminder like (agentA put comment : i will work on this comment tomorrow). so this comment will display to only agentA.
for this implementation I have created a new table named complaint_detail where I add coloumn 'comment' and 'user_id'
and to display complaints i write query
select complaint.Complaint_name,complaint.User_ID from complaint
left outer join complaint_detail on complaint.Complaint_id = complaint_detail.complaint_id
this query display all the records now when I filter on user it will show only that users record to solve this I add
select * from (select complaint.Complaint_name,complaint.User_ID from complaint
left outer join complaint_detail on complaint.Complaint_id = complaint_detail.complaint_id
complaint_detail.complaint_info_id
) asdf
where user_id = 'agentA'
or User_ID is null
select * from (
select complaints.complaint_id,complaints.complaint_detail, complaints_detail.comment,complaints_detail.user_id from complaints
left outer join complaints_detail on complaints.Complaint_id = complaints_detail.complaint_id
) asdf
where user_id = 'agentA'
or User_ID is null
is
complaint_id complaint_detail comment user_id
1 complaint_1 complaint_1 agentA
2 complaint_2 complaint_2 agentA
3 complaint_3 null null
for agentB
complaint_id complaint_detail comment user_id
1 complaint_1 complaint1_ agentB
3 complaint_3 null null
any idea how can i achieve this that every user can see all complaints and only their comments.should i change table structure or query can do this ?
Something like this should do it:
select * from complaints cmp
left outer join comments com on cmp.id=com.complaint_id
and com.user_id='agentA' or com.user_id is null
This will get data from comments table related to complaint if it exists (left join)
and limit comments to those of the agent or without a user id on the comment
Of course you can specify columns in the select if you don't want to retrieve all columns from the complaints & comments tables.
I have three tables that I need to join together and get a combination of results. I have tried using left/right joins but they don't give the desired results.
For example:
Table 1 - STAFF
id name
1 John
2 Fred
Table 2 - STAFFMOBILERIGHTS
id staffid mobilerightsid rights
--this table is empty--
Table 3 - MOBILERIGHTS
id rightname
1 Login
2 View
and what I need is this as the result...
id name id staffid mobilerightsid rights id rightname
1 John null null null null 1 login
1 John null null null null 2 View
2 Fred null null null null 1 login
2 Fred null null null null 2 View
I have tried the following :
SELECT *
FROM STAFFMOBILERIGHTS SMR
RIGHT JOIN STAFF STA
ON STA.STAFFID = SMR.STAFFID
RIGHT JOIN MOBILERIGHTS MRI
ON MRI.ID = SMR.MOBILERIGHTSID
But this only returns two rows as follows:
id name id staffid mobilerightsid rights id rightname
null null null null null null 1 login
null null null null null null 2 View
Can what I am trying to achieve be done and if so how?
Thanks
From your comment its now clear you want a cross join (include all rows from staff and mobilerights). Something like this should do it
SELECT
*
FROM Staff, MobileRights
LEFT OUTER JOIN StaffMobileRights ON StaffMobileRights.StaffId = Staff.Id
The FROM clause specifies that we will be including all rows from the Staff table, and all rows from the MobileRights table. The end result will therefore contain (staff * MobileRights) rows.
To bring in rows from StaffMobileRights then we need a join to that table also. We use a LEFT OUTER join to ensure that we always include the left side (rows in the staff table) but we arent too bothered if no rows exist on the right side (StaffMobileRights table). If no row exists for the join then null values are returned.
What you are probably asking is to see null where is no rights. In the rectangular style that results are always returned, this is the only way to represent it with a simple join:
From PaulG's query i changed it a bit to always get everything form the STAFF table.
SELECT
*
FROM STAFF
RIGHT OUTER JOIN StaffMobileRights ON StaffMobileRights.StaffId = Staff.Id
INNER JOIN MobileRights ON MobileRights.Id = StaffMobileRights.MobileRightsId