我試圖獲得按年份和國家/地區分組的總銷售額。這按預期工作:使用GROUP BY和OVER
SELECT Year, Country, SUM([Total Sales])
FROM Table
GROUP BY Country, Year;
然後,我必須比較每個國家對一年總銷售額的貢獻。我這樣做:
SELECT Year, Country, SUM([Total Sales]),
SUM([Total Sales]) OVER(PARTITION BY Year)
FROM Table
GROUP BY Country, Year;
這給了我這個錯誤:
Column 'Table.Total Sales' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
您的第一句話是誤導性的,因爲您可以在不使用子查詢的情況下合併窗口函數和聚合函數。 –