[英]Access 2010 SQL select top with an additional field
This gives me the top 5 category/year combinations by spending: 通过支出,我可以得出前5个类别/年度组合:
SELECT TOP 3 [Category],
SUM([Spending]),
Year
FROM Data
GROUP BY [Category], [Year]
;
I would like to look at the overall top 5 categories for every year. 我想每年查看一下总体排名前5位的类别。
eg If the top spending categories over the 3 year period are A,B,C (but not D) I would like to output this: 例如,如果三年期间的最高支出类别是A,B,C(但不是D),我想输出以下内容:
Category Total Spending Year
A 123 1
A 321 2
A 987 3
B 798 1
B 465 2
B 153 3
C 654 1
C 486 2
C 759 3
This is solved here, but only with two fields- SQL: Select Top 3 Records + Sum of Quantity 这可以在此处解决,但只能使用两个字段来解决-SQL:选择前3条记录+数量总和
Try in this way: 以这种方式尝试:
SELECT [Category], SUM([Spending]) as TotalSpending, Year
FROM Data
WHERE Year IN (SELECT TOP 5 Year FROM Data ORDER BY Year DESC)
GROUP BY [Category], [Year]
ORDER BY SUM([Spending]) DESC, [Year] DESC
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.