花費超過3小時後,我放棄了。選擇總計只返回SQL Server中的一條記錄
我有四個表:
Users, Approvals, Centers, Managements
我的最終目標是讓用戶在用戶角色分開各自管理的總數量(我有兩個角色在這裏:家長和社會成員)
我一直在使用下面的代碼
select
(select count(r.StudentId)
from Users u
where u.UserId = r.StudentId and u.RoleId = 10) as Parents,
(select count(r.StudentId)
from Users u
where u.UserId = r.StudentId and u.RoleId = 11) as SocietyMembers,
e.ManagementId, e.ManagmentTitle
from
CentersRegistrationsApprovals r --ON r.StudentId = u.UserId
inner join
Centers c ON c.CenterId = r.CenterId
inner join
Managments e ON e.ManagementId = c.EducationManagementId
group by
e.ManagementId, e.ManagmentTitle, StudentId
我期待的查詢結果是因爲以下幾點:
Parents SocietyMambers ManagementId ManagementName
----------------------------------------------------------------
3 3 10 North Region
但結果總是被設置給了我
Parents SocietyMambers ManagementId ManagementName
----------------------------------------------------------------
3 NULL 10 North Region
NULL 3 10 North Region
任何想法如何鞏固結果只有1記錄?
刪除組由 –
@KannanKandasamy那麼你不能在SELECT子句中使用它 –