我希望找到按年度分组的结果及过去3年的平均金额,使用tsql来回到2位小数
Environment.GetLogicalDrives()
答案 0 :(得分:1)
使用avg() over()
来获取过去三年的平均值,来自平均当年金额的子查询。
select
year
, amount = avg(amount) over (order by year rows 2 preceding)
from (
select year, amount = avg(convert(decimal(19,2),amount))
from t
group by year
) as s
order by year desc
rextester演示:http://rextester.com/SUUP93797
返回:
+------+------------+
| year | amount |
+------+------------+
| 2017 | 183.333333 |
| 2016 | 166.666666 |
| 2015 | 133.333333 |
| 2014 | 100.000000 |
| 2013 | 100.000000 |
+------+------------+