Reputation: 6758
UsersInGroup table
UserName, GroupName
UserReports table
UserName, ReportName, IsShared
I would like to run a query to find out a user shared reports for other users in the same group.
SELECT ur.ReportName FROM UserReports ur
JOIN UsersInGroups ug ON ur.UserName = ug.UserName
WHERE ur.IsShared =1 AND
ug.GroupName =(SELECT GroupName UsersInGroups WHERE UsersInGroups.UserName='admin')
But this query says "ambiguous 'multi-part identifier' could not be bound?"
The solution I can think of is to add GroupName column into UserReports table to make the query simple. However this seems to be redundant column.
What is this query type called and how can I write the query to get the correct result?
Upvotes: 0
Views: 67
Reputation: 201
You're missing a from and maybe you should think about putting a "IN" clause instead of "=".
...
ug.GroupName IN (SELECT GroupName
...
It'll prevent the query from crashing if there are severals users with the same name...
Upvotes: 0
Reputation: 518
Try this query,
SELECT ur.ReportName FROM UserReports ur JOIN UsersInGroups ug ON ur.UserName = ug.UserName WHERE ur.IsShared =1 AND ug.GroupName =(SELECT uig.GroupName from UsersInGroups uig WHERE uig.UserName='admin')
Upvotes: 0
Reputation: 72175
Your subquery is missing a FROM
:
SELECT ur.ReportName
FROM UserReports ur
JOIN UsersInGroups ug ON ur.UserName = ug.UserName
WHERE ur.IsShared = 1 AND
ug.GroupName = (SELECT GroupName
FROM UsersInGroups
WHERE UsersInGroups.UserName='admin')
Upvotes: 4