SQL Server:
SELECT tblModule.Title , Count (tblActivity.Name) AS NumberOfActivities
From tblActivity
INNER JOIN tblModule
ON tblModule.ModuleID = tblActivity.ModuleID
Order by tblModule.Title
在这里,我试图显示每个模块标题的名称在tblActivity中显示的次数,我将如何做到这一点错误是:
Msg 8120, Level 16, State 1, Line 1
Column 'tblModule.Title' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
实际问题:对于每个模块,列出模块标题和为模块安排的活动数量。(5)
答案 0 :(得分:1)
SELECT m.Title, COUNT_BIG(a.Name) AS NumberOfActivities
FROM dbo.tblActivity a
JOIN dbo.tblModule m ON m.ModuleID = a.ModuleID
GROUP BY m.Title
ORDER BY m.Title
或
SELECT m.Title, NumberOfActivities
FROM (
SELECT ModuleID, COUNT_BIG(*) AS NumberOfActivities
FROM dbo.tblActivity
GROUP BY ModuleID
) a
JOIN dbo.tblModule m ON m.ModuleID = a.ModuleID
ORDER BY m.Title
无论如何,请提供实际和例外的结果......