SSRS - 基于组值的动态表达

时间:2017-08-02 14:53:38

标签: dynamic reporting-services expression

问题:如何在SSRS中创建一个动态表达式,以引用列之前的单元格值?

表:该表是按日期使用列分组构建的。

使用的软件:使用Visual Studio 2015报表服务器项目。

示例:查看屏幕截图。

  1. 这是SSRS中的设计视图:
  2. [![在此处输入图像说明] [1]] [1]

    1. 结果如下:
    2. enter image description here

      1. 这是数据集的一个示例。请注意,起始余额是在第一天,然后我希望它延续到第二天minis剩余ID的总和。 [enter image description here 3

1 个答案:

答案 0 :(得分:-1)

也许这可以帮到你

DECLARE @TestData TABLE
    (
        DateColumn DATE ,
        ID INT ,
        Type NVARCHAR(100) ,
        Ammount DECIMAL(18, 2)
    );

INSERT INTO @TestData (   DateColumn ,
                          ID ,
                          Type ,
                          Ammount
                      )
VALUES ( '8/2/2017', 1, N'1_BeginningCash', 1797791 ) ,
    ( '8/4/2017', 2, N'2_AR', 41526.23 ) ,
    ( '8/4/2017', 5, N'5_PaymentOther', -19863.21 ) ,
    ( '8/5/2017', 3, N'3_Vendor_Rebates', 1200 ) ,
    ( '8/7/2017', 2, N'2_AR', 12163.94 ) ,
    ( '8/9/2017', 2, N'2_AR', 179236.8 ) ,
    ( '8/9/2017', 4, N'4_Payment_Supplier', -1066266.32 );

WITH FirstLevel
AS ( SELECT DateColumn ,
            ID ,
            Type ,
            Ammount ,
            SUM(Ammount) OVER ( PARTITION BY DateColumn ) AS DateTotal ,
            SUM(Ammount) OVER ( ORDER BY DateColumn ,
                                         ID
                              ) AS RunningTotal
     FROM   @TestData
   ) ,
     SecondLevel
AS ( SELECT FirstLevel.DateColumn ,
            FirstLevel.ID ,
            FirstLevel.Type ,
            FirstLevel.Ammount ,
            FirstLevel.DateTotal ,
            FirstLevel.RunningTotal ,
            LAG(FirstLevel.RunningTotal, 1, 0) OVER ( ORDER BY FirstLevel.DateColumn ,
                                                               FirstLevel.ID
                                                    ) LastRunningTotal
     FROM   FirstLevel
   ) ,
     ThirdLevel
AS ( SELECT SecondLevel.DateColumn ,
            SecondLevel.ID ,
            SecondLevel.Type ,
            SecondLevel.Ammount ,
            SecondLevel.DateTotal
            + FIRST_VALUE(SecondLevel.LastRunningTotal) OVER ( PARTITION BY SecondLevel.DateColumn
                                                               ORDER BY SecondLevel.DateColumn ,
                                                                        SecondLevel.ID
                                                             ) AS DateTotal ,
            FIRST_VALUE(SecondLevel.LastRunningTotal) OVER ( PARTITION BY SecondLevel.DateColumn
                                                             ORDER BY SecondLevel.DateColumn ,
                                                                      SecondLevel.ID
                                                           ) AS Beginning
     FROM   SecondLevel
   )
SELECT *
FROM   ThirdLevel;