我有一個選擇,它看起來像以下:GROUP BY CASE帶 - TSQL
SELECT
ReportingDate
, PortfolioID
, PortfolioNme
, CASE
WHEN @ReportType = 'GeoCountry' THEN Infoportal.dbo.fn_Generic_ProperCase(Country)
WHEN @ReportType = 'GeoEquity' THEN Region
END AS Country
, RANK() OVER (PARTITION BY PortfolioID ORDER BY SUM(Percentage) DESC, CASE WHEN @ReportType = 'GeoCountry' THEN Country WHEN @ReportType = 'GeoEquity' THEN Region END) AS [Rank]
, SUM(Percentage) AS [Weight]
FROM @Worktable as WT
WHERE WT.IssueType1 <> '010' AND WT.IssueType2 <> '055'
GROUP BY WT.ReportingDate
, WT.PortfolioID
, WT.PortfolioNme
, CASE
WHEN @ReportType = 'GeoCountry' THEN WT.Country
WHEN @ReportType = 'GeoEquity' THEN WT.Region
END
我想要做的是按國家或地區根據@ReportType和顯示的總和百分比和排名。
不過,我不斷收到錯誤:
Msg 8120, Level 16, State 1, Line 349
Column '@Worktable.Country' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Msg 8120, Level 16, State 1, Line 350
Column '@Worktable.Region' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
這是很容易的,這是真的討厭我。幫助將不勝感激。
感謝
知道這將是容易的,週五下午的大腦凍結。隊友的歡呼聲。 – 2013-03-01 16:44:15
@chrissyp不客氣... – 2013-03-01 16:44:47