我想創建一個基本的排名並將存儲過程的結果從最顯示的主講人排序到最低。SQL Server:通過此計算計數總數和順序
我試過以下工作正常,如果我刪除groupCount和GROUP BY,但我不能讓它包括這個,以便由演示者分組。 基本上我想看到的是呈現最多(即groupCount的最大值)的人,然後是最低的人(即groupCount的最小值)。
我得到的錯誤是:
Msg 8120, Level 16, State 1, Procedure CountPresenters, Line 17
Column 'MeetingDetails.topic' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
我保存至今的過程:
ALTER PROCEDURE [dbo].[CountPresenters]
@title nvarchar(200)
AS
BEGIN
SET NOCOUNT ON;
SELECT B.presenter,
B.topic,
A.meetingDate,
A.title,
COUNT(*) AS groupCount
FROM MeetingDetails B
INNER JOIN MeetingDates A
ON B.meetingID = A.meetingID
WHERE B.itemStatus = 'active'
AND A.title LIKE '%'[email protected]+'%'
GROUP BY B.presenter
ORDER BY groupCount desc, B.presenter, A.meetingDate desc
FOR XML PATH('presenters'), ELEMENTS, TYPE, ROOT('ranks')
END
非常感謝任何幫助,蒂姆。
MAX(B.topic),? – StanislavL
與加入無關,如果您聚合一列,則所有列也必須彙總或分組。 –