我有一个选择,它看起来像以下: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