如果你想要累计金额,那么我希望:
select ColumnDescription, YearMonth, sum(TotalMinutes) as TotalMinutes,
sum(sum(TotalMinutes)) over (partition by ColumnDescription order by YearMonth) as running_TotalMinutes
from MyTable
where yearmonth between '201704' and '201706'
group by ColumnDescription, YearMonth
order by yearmonth;
如果你想连续多年这样做,那么你需要提取财政年度。这有点麻烦,但可行:
select ColumnDescription, YearMonth, sum(TotalMinutes) as TotalMinutes,
sum(sum(TotalMinutes)) over (partition by ColumnDescription, v.fiscal_year order by YearMonth) as running_TotalMinutes
from MyTable t cross apply
(values (case when right(yearmonth, 2) >= '04' then cast(left(yearmonth, 4) as int)
else cast(left(yearmonth, 4) as int) - 1
end)
) v(fiscal_year)
group by ColumnDescription, YearMonth
order by yearmonth;