我有以下Group By
SQL:
SELECT
s.Login_Name, COUNT(s.s1CIDNumber)
FROM [dbSuppHousing].[dbo].[tblSurvey] s
group by s.Login_Name
我想知道如何将此结果集加入另一个表(tblUsers
)以将user_id添加到结果集(tblSurvey
并且tblUsers
在Login_Name上具有1-1关系)
我尝试了以下内容:
Select u.User_Id from tblUsers u,a.Login_Name
inner join
(SELECT
s.Login_Name Login_Name, COUNT(s.s1CIDNumber)as abc
FROM [dbSuppHousing].[dbo].[tblSurvey] s
group by s.Login_Name) a
on
u.Login_Name=a.Login_Name
我收到错误。问题是 a
的列在外面不可见。
例如a.abc
答案 0 :(得分:1)
你错了from tblUsers u,a.Login_Name
尝试移动这段代码a.Login_Name
来选择
Select u.User_Id, a.Login_Name from tblUsers u
inner join
(SELECT
s.Login_Name Login_Name, COUNT(s.s1CIDNumber)as abc
FROM [dbSuppHousing].[dbo].[tblSurvey] s
group by s.Login_Name) a
on
u.Login_Name=a.Login_Name
答案 1 :(得分:0)
您的from
条款位置错误:
Select u.User_Id, a.*
from tblUsers u inner join
(SELECT s.Login_Name, COUNT(s.s1CIDNumber) as abc
FROM [dbSuppHousing].[dbo].[tblSurvey] s
GROUP BY s.Login_Name
) a
ON u.Login_Name = a.Login_Name;
答案 2 :(得分:0)
from应该引用一个表而不是一列:
Select u.User_Id, a.Login_Name from tblUsers u
inner join
除此之外它应该有用。
答案 3 :(得分:0)
有些事情应该这样做。
select
s.Login_Name,
u.col_a,
u.col_b,
u.col_c,
count(s.s1CIDNumber)
from
[dbSuppHousing].[dbo].[tblSurvey] s
join tblUsers t on s.user_id = t.user_id
group by
s.Login_Name
u.col_a,
u.col_b,
u.col_c