【问题标题】:How to sum of particular column with month and year with fiscal year如何将特定列与月份和年份与财政年度相加
【发布时间】:2020-07-20 20:18:45
【问题描述】:

我的表中有以下数据:

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 版本不支持它。

我该怎么做?

【问题讨论】:

    标签: sql-server tsql date sql-server-2008 pivot


    【解决方案1】:

    这与your previous question 非常相似。在这里,您可以将日期偏移 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-26
      • 1970-01-01
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多