答案 0 :(得分:1)
如果你这样做,那是可以实现的:
第1步。对于您的源代码,您可以构建一个SQL查询,您可以按年,AbsoluteMonth等对数据进行分组。
因此,对于每个Year / AbsoluteMonth对,报告只有 ONE 值。
第2步。使用以下公式:
=IIf(IsNothing(Sum(Fields!Amt.Value)), Last(Fields!Amt.Value, "Year"), Sum(Fields!Amt.Value))
这里"年"是组名,Amt - 你的字段名,可能是R_Pax
<强>步骤3 即可。 (可选)如果数据不是自然排序的,则对数据进行排序,以提供正确的最后一个值。
第1步非常重要。否则,具有空值的单元格将不会显示最后的总计,它将显示一个月的最后一个值,因此如果month(1)具有值30,50,60和month(2)则不会#39; t有任何值,然后它会显示60个月(2),月(3)等...,而不是总和(30 + 50 + 60)。
答案 1 :(得分:1)
您最好将剩余的空白记录与最后一个值一起插入数据集,然后再将数据传递给报告。
我假设您的表格是矩阵。
DECLARE @Today DATETIME
SET @Today = GETDATE()
DECLARE @MatrixData TABLE (
Month1 INT
, Year1 INT
, Value INT
)
INSERT INTO @MatrixData (Month1, Year1, Value)
SELECT MONTH(DATEADD(MONTH, Id * -1, @Today)) AS Date1Month, YEAR(DATEADD(MONTH, Id * -1, @Today)) AS Date1Year, Id * 10 AS Value1
FROM (
SELECT TOP 60 ROW_NUMBER() OVER (ORDER BY Id) AS Id
FROM SysObjects
) A
ORDER BY Date1Year, Date1Month
SELECT * FROM @MatrixData
-- Insert blank month of last year with last value
INSERT INTO @MatrixData (Month1, Year1, Value)
SELECT A.RunningMonth, A1.MaxYear, A1.LastValue
FROM (
SELECT TOP 12 ROW_NUMBER() OVER (ORDER BY Id) AS RunningMonth
FROM SysObjects
) A
INNER JOIN (
-- Get Last Value in @MatrixData
SELECT A.MinMonth, A.MaxMonth, A.MaxYear, A1.Value AS LastValue
FROM (
-- Get Max Month Last Year in @MatrixData
SELECT MAX(A1.Month1) AS MinMonth, A.MaxMonth, A.MaxYear
FROM (
-- Get Max Month & Max Year
SELECT MAX(Month1) AS MaxMonth, MAX(Year1) AS MaxYear
FROM @MatrixData
) A
INNER JOIN @MatrixData A1 ON A.MaxYear = A1.Year1
GROUP BY A.MaxMonth, A.MaxYear
) A
INNER JOIN @MatrixData A1 ON A.MinMonth = A1.Month1 AND A.MaxYear = A1.Year1
) A1 ON A.RunningMonth > A1.MinMonth AND A.RunningMonth <= A1.MaxMonth
SELECT * FROM @MatrixData
答案 2 :(得分:0)
在Oracle中,它就像这样完成。 SQL Server具有COALESCE和LAG功能。因此,SQL Server也必须这样做。还有另一个类似于此的stackoverflow问题。无法找到它。
create table mytab(n number, m number);
insert into mytab values(1,null);
insert into mytab values(2,null);
insert into mytab values(3,44949);
insert into mytab values(4,null);
insert into mytab values(5,null);
insert into mytab values(6,null);
insert into mytab values(7,null);
insert into mytab values(8,null);
insert into mytab values(9,null);
insert into mytab values(10,null);
insert into mytab values(11,74631);
insert into mytab values(12,null);
insert into mytab values(13,null);
select t.*, coalesce(m, lag(m ignore nulls) over (order by n))
from mytab t;
答案 3 :(得分:0)
我们可以在SQL端执行此操作并将数据提取到SSRS
步骤:
然后使用SQL方法重新生成空值和最后一个值为空的最后一个值 价:
`select a.AbsoluteMonth,Mon
,first_value(a.S1_pax)over(partition by a.v1_p order by num ) as S_Pax
,first_value(a.S2_pax)over(partition by a.v2_p order by num ) as S2_Pax`
from
(select *
,sum(case when S1_pax is null then 0 else 1 end) over (order by num) as v1_p
,sum(case when S2_pax is null then 0 else 1 end) over (order by num) as v2_p
from X_Table
)a
分别填写所有地方。 Plz参考下面的输出