enter image description here我需要一些关于如何计算年初至今的建议。我用黄色突出显示了我想在报告中计算的YTD值。这个问题是我之前发布的问题的延续,以计算总计标签.- Totals in Matrix in SSRS
我还提到了用于找出这些值的公式。enter image description here
答案 0 :(得分:0)
好吧,经过深思熟虑,我想我会在我的SQL中这样做,因为我在那里更舒服。以下是我将如何做到这一点的示例:
declare @tbl table
(
Country varchar(2),
Type varchar(1),
Names varchar(5),
Qty int,
DateValue datetime
)
insert into @tbl
values ('US', 'A','BB-1',10,'12/01/2015'),
('US', 'A','BB-2',20,'12/01/2015'),
('IN', 'A','BB-1',0,'12/01/2015'),
('IN', 'A','BB-2',10,'12/01/2015'),
('US', 'A','BB-1',30,'12/01/2016'),
('US', 'A','BB-2',40,'12/01/2016'),
('IN', 'A','BB-1',50,'12/01/2016'),
('IN', 'A','BB-2',70,'12/01/2016')
select
t.*,
YearEnd.YearEnd ,
case when YearEnd.YearEnd = 0 then null else (t.Qty - YearEnd.YearEnd)/convert(decimal(36,4),YearEnd.YearEnd) end as YearEndCalc
from @tbl as t
left join
(
select
year(DateValue) as YearValue,
Type,
sum(Qty) as YearEnd
from @tbl
where
month(DateValue) = 12
group by
year(DateValue) ,
Type
) as YearEnd
on YearEnd.YearValue = year(t.datevalue)-1
and YearEnd.Type = t.Type