[英]Sum of particular column with month and year with fiscal year from custom date
我的表中有以下数据:
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
8 2019-02-01 60
9 2020-01-20 100
现在,当我输入start date = '2018-03-12'
时,我的财政年度必须从march 2018 to feb 2019
开始,依此类推。
如果我输入start date = '2019-05-12' then my fiscal year must start with May 2019 to April 2020
我已经尝试过以下查询,但它无法正常工作,并且它计算过去的一年,即 2017 年我不希望从我输入的自定义日期获得过去一年的任何数据。 因此,如果输入的开始日期 = '2018-03-12',则必须仅开始计算 2018 年及以上年份。 没有过去的一年。
Declare @startdate as date
Declare @monthDate as int
Declare @ownmonth as int
set @startdate = '2018-03-12'
set @monthDate = month(@startdate)
set @ownmonth = 1
select
year(dateadd(month, -@monthDate, d_date)) year,
sum(case when month(d_date) = case when @monthDate+1 > 12 then @ownmonth else @monthDate+1 End then amount end) ,
sum(case when month(d_date) = case when @monthDate+2 > 12 then @ownmonth+1 else @monthDate+2 End then amount end) ,
sum(case when month(d_date) = case when @monthDate+3 > 12 then @ownmonth+2 else @monthDate+3 End then amount end) ,
sum(case when month(d_date) = case when @monthDate+4 > 12 then @ownmonth+3 else @monthDate+4 End then amount end) ,
sum(case when month(d_date) = case when @monthDate+5 > 12 then @ownmonth+4 else @monthDate+5 End then amount end) ,
sum(case when month(d_date) = case when @monthDate+6 > 12 then @ownmonth+5 else @monthDate+6 End then amount end) ,
sum(case when month(d_date) = case when @monthDate+7 > 12 then @ownmonth+6 else @monthDate+7 End then amount end) ,
sum(case when month(d_date) = case when @monthDate+8 > 12 then @ownmonth+7 else @monthDate+8 End then amount end) ,
sum(case when month(d_date) = case when @monthDate+9 > 12 then @ownmonth+8 else @monthDate+9 End then amount end) ,
sum(case when month(d_date) = case when @monthDate+10 > 12 then @ownmonth+9 else @monthDate+10 End then amount end) ,
sum(case when month(d_date) = case when @monthDate+11 > 12 then @ownmonth+10 else @monthDate+11 End then amount end) ,
sum(case when month(d_date) = case when @monthDate+12 > 12 then @ownmonth+11 else @monthDate+12 End then amount end) ,
sum(amount) total
from mytable
group by year(dateadd(month, -@monthDate, amount))
order by year
但是上面的查询没有显示正确的年份和月份数据
现在我想要 output 与会计年度计算:
Year Mar Apr May Jun July Aug Sept Oct Nov Dec Jan Feb Total
2018 - - - 100 - - - - - 6950 - 60 7110
2019 456.5 - 200.48 - 200 - - - - - 100 - 956.98
我不能使用PIVOT
,因为我的紧凑型 SQL 服务器版本不支持它。
我怎样才能做到这一点?
您的会计年度规则是当年的 3 月到次年的 2 月:
date | fiscal year ... | ... 2018-02-28 | 2017 2018-03-01 | 2018 ... | 2018 2019-02-28 | 2018 2019-03-01 | 2019 ... | ...
这意味着当我们从日期中减去两个月时,我们会得到一个日期,其年份是财政年度:
date | date - 2 months | fiscal year ... | ... | ... 2018-02-28 | 2017-12-28 | 2017 2018-03-01 | 2018-01-01 | 2018 ... | ... | 2018 2019-02-28 | 2018-12-28 | 2018 2019-03-01 | 2019-01-01 | 2019 ... | ... | ...
select
year(dateadd(month, -2, d_date)) as fiscal_year,
sum(case when month(d_date) = 3 then amount else 0 end) as mar,
sum(case when month(d_date) = 4 then amount else 0 end) as apr,
sum(case when month(d_date) = 5 then amount else 0 end) as may,
sum(case when month(d_date) = 6 then amount else 0 end) as jun,
sum(case when month(d_date) = 7 then amount else 0 end) as jul,
sum(case when month(d_date) = 8 then amount else 0 end) as aug,
sum(case when month(d_date) = 9 then amount else 0 end) as sep,
sum(case when month(d_date) = 10 then amount else 0 end) as oct,
sum(case when month(d_date) = 11 then amount else 0 end) as nov,
sum(case when month(d_date) = 12 then amount else 0 end) as dec,
sum(case when month(d_date) = 1 then amount else 0 end) as jan,
sum(case when month(d_date) = 2 then amount else 0 end) as feb,
sum(amount) as total
from mytable
group by year(dateadd(month, -2, d_date))
order by year(dateadd(month, -2, d_date));
如果您想将此限制为给定日期所在的会计年度,请添加:
where year(dateadd(month, -2, d_date)) = year(dateadd(month, -2, @given_date))
好吧,如果你想把它限制在从那一年开始的财政年度,那当然是:
where year(dateadd(month, -2, d_date)) >= year(dateadd(month, -2, @given_date))
更新:您希望一个会计年度从给定日期当月的第一天开始。 即,如果给定日期是 1990-04-23,则会计年度从 4 月开始。 上面的查询仅略有变化,因为我们必须将其概括为比给定月份少一个月,而不是减去 2 个月(对于 3 月)。
我在比较月份时使用了模运算,以免以 13、14 等月份结束。
select
year(dateadd(month, - month(@startdate) + 1, d_date)) as fiscal_year,
sum(case when month(d_date) = (month(@startdate) + 0) % 12 then amount else 0 end) as first,
sum(case when month(d_date) = (month(@startdate) + 1) % 12 then amount else 0 end) as second,
sum(case when month(d_date) = (month(@startdate) + 2) % 12 then amount else 0 end) as third,
sum(case when month(d_date) = (month(@startdate) + 3) % 12 then amount else 0 end) as fourth,
sum(case when month(d_date) = (month(@startdate) + 4) % 12 then amount else 0 end) as fith,
sum(case when month(d_date) = (month(@startdate) + 5) % 12 then amount else 0 end) as sixth,
sum(case when month(d_date) = (month(@startdate) + 6) % 12 then amount else 0 end) as seventh,
sum(case when month(d_date) = (month(@startdate) + 7) % 12 then amount else 0 end) as eighth,
sum(case when month(d_date) = (month(@startdate) + 8) % 12 then amount else 0 end) as nineth,
sum(case when month(d_date) = (month(@startdate) + 9) % 12 then amount else 0 end) as tenth,
sum(case when month(d_date) = (month(@startdate) + 10) % 12 then amount else 0 end) as eleventh,
sum(case when month(d_date) = (month(@startdate) + 11) % 12 then amount else 0 end) as twelfth,
sum(amount) as total
from mytable
group by year(dateadd(month, - month(@startdate) + 1, d_date))
order by year(dateadd(month, - month(@startdate) + 1, d_date));
同样,如果我们希望我们的结果从给定日期的财政年度开始,我们会添加:
where year(dateadd(month, - month(@startdate) + 1, d_date)) >= year(@startdate)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.