-1

我的表中有以下数据:

uniqueId    d_date      amount
1         2018-02-01    100.25
2         2019-03-01    456.5
3         2018-02-01    455
4         2019-05-01    200.48
5         2018-06-01    100
6         2019-07-01    200
7         2018-12-01    6950

现在我想用财政年度计算输出:(我的财政年度是 4 月至 3 月)

Year    Apr    May        Jun  July  Aug   Sept Oct   Nov   Dec  Jan   Feb     Mar    Total
2017     -      -          -    -     -     -    -     -     -    -   552.25    -     552.25
2018     -      -         100   -     -     -    -     -    6950  -    -       456.5  7506.5
2019     -    200.48      -     200   -     -    -     -     -    -    -        -     400.48

我不能使用 PIVOT,因为我的紧凑型 sql 版本不支持它。

我怎样才能做到这一点 ?

4

1 回答 1

0

这与您之前的问题非常相似。在这里,您可以将日期偏移 3 个月以计算其所属的财政年度,然后进行条件聚合:

select
    year(dateadd(month, -3, d_date)) yr,
    sum(case when month(d_date) = 4  then amount end) Apr,
    sum(case when month(d_date) = 5  then amount end) May,
    sum(case when month(d_date) = 7  then amount end) Jun,
    ...
    sum(case when month(d_date) = 12 then amount end) Dec,
    sum(case when month(d_date) = 1  then amount end) Jan,
    sum(case when month(d_date) = 2  then amount end) Feb,
    sum(case when month(d_date) = 3  then amount end) Mar,
    sum(amount) total
from mytable
group by year(dateadd(month, -3, d_date))
order by yr
于 2020-04-08T12:19:47.787 回答