如何获取SSRS中行的先前值

时间:2016-10-06 11:41:30

标签: sql-server reporting-services ssrs-2008-r2 ssrs-2012

我的SSRS报告有两个组(帐户,月)。帐户是父级,月份是子组。现在我想将每个月的期末余额显示为下个月的期初余额。下面给出了一份样本报告。红色字体表示每个组的SUM。请记住,我试图通过使用SSRS Previous()函数获得结果,但无法获得预期的结果。

Previous(Sum(Fields!NetAmt.Value),"Month")

月=>月份组名称。

任何人都可以帮助我吗?

提前致谢。

拉​​希德

示例SQL数据

CREATE TABLE [dbo].[Balances](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [Account] [nvarchar](50) NULL,
    [Month] [date] NULL,
    [BegBalance] [float] NULL,
    [Debit] [float] NULL,
    [Credit] [float] NULL,
    [EndBalance] [float] NULL,
 CONSTRAINT [PK_Balances] PRIMARY KEY CLUSTERED 
(
    [id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[Balances] ON
INSERT [dbo].[Balances] ([id], [Account], [Month], [BegBalance], [Debit], [Credit], [EndBalance]) VALUES (1, N'Cash', CAST(0xDB3A0B00 AS Date), 0, 100, 50, 50)
INSERT [dbo].[Balances] ([id], [Account], [Month], [BegBalance], [Debit], [Credit], [EndBalance]) VALUES (2, N'Cash', CAST(0xDB3A0B00 AS Date), 0, 200, 50, 150)
INSERT [dbo].[Balances] ([id], [Account], [Month], [BegBalance], [Debit], [Credit], [EndBalance]) VALUES (3, N'Cash', CAST(0xFA3A0B00 AS Date), 0, 200, 50, 150)
INSERT [dbo].[Balances] ([id], [Account], [Month], [BegBalance], [Debit], [Credit], [EndBalance]) VALUES (4, N'Cash', CAST(0xFA3A0B00 AS Date), 0, 200, 100, 100)
INSERT [dbo].[Balances] ([id], [Account], [Month], [BegBalance], [Debit], [Credit], [EndBalance]) VALUES (5, N'Mr.  Axxx', CAST(0xDB3A0B00 AS Date), 0, 200, 100, 100)
INSERT [dbo].[Balances] ([id], [Account], [Month], [BegBalance], [Debit], [Credit], [EndBalance]) VALUES (6, N'Mr.  Axxx', CAST(0xDB3A0B00 AS Date), 0, 100, 50, 50)
INSERT [dbo].[Balances] ([id], [Account], [Month], [BegBalance], [Debit], [Credit], [EndBalance]) VALUES (7, N'Mr.  Axxx', CAST(0xFA3A0B00 AS Date), 0, 100, 50, 50)
INSERT [dbo].[Balances] ([id], [Account], [Month], [BegBalance], [Debit], [Credit], [EndBalance]) VALUES (8, N'Mr.  Axxx', CAST(0xFA3A0B00 AS Date), 0, 100, 20, 80)
SET IDENTITY_INSERT [dbo].[Balances] OFF

使用Previous()函数后 enter image description here

2 个答案:

答案 0 :(得分:1)

您可以在数据集的SQL查询中处理它,尝试使用LAG()

SELECT mt.*, beg.begbal FROM [dbo].[Balances] mt
JOIN
(
SELECT [Account]
      ,[Month]
      ,SUM([EndBalance]) EndBal
      ,LAG (SUM([EndBalance]), 1, 0) OVER (PARTITION BY [Account] ORDER BY [Month] ASC) begbal
  FROM [dbo].[Balances]
  GROUP BY [Account]
      ,[Month]
) beg
ON mt.Month = beg.Month
AND mt.Account = beg.Account

更新:,不使用LAG(),请尝试以下代码。

WITH CTE AS
(
SELECT [Account]
      ,[Month]
      ,SUM([EndBalance]) EndBal
      ,ROW_NUMBER() OVER (PARTITION BY [Account] ORDER BY [Month] ASC) RowVal
  FROM [dbo].[Balances]
  GROUP BY [Account]
      ,[Month]
)

SELECT mt.*, ISNULL(t2.EndBal, 0) as begbal FROM CTE t1
LEFT JOIN CTE t2
ON t1.Account = t2.Account AND t1.RowVal = t2.RowVal + 1
JOIN [dbo].[Balances] mt
ON t1.Month = mt.Month AND t1.Account = mt.Account

此外,下面是您要查找的SSRS表达式代码:

=IIF
(
(RunningValue(Fields!Month.Value, CountDistinct, "Account")) = 1,
0,
Previous(SUM(Fields!EndBalance.Value),"MonthGrp")
)

答案 1 :(得分:1)

您可以使用private IWebDriver GetSauceLabsDriver(){ var outPutDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); ChromeOptions options = new ChromeOptions(); options.AddExtensions(outPutDirectory + @"\3.1.3_0.crx"); //DesiredCapabilities caps = (DesiredCapabilities)options.ToCapabilities(); var caps = new DesiredCapabilities(); caps.SetCapability(ChromeOptions.Capability, options.Extensions[0]); caps.SetCapability(CapabilityType.BrowserName, "chrome"); caps.SetCapability(CapabilityType.Version, "53.0"); caps.SetCapability(CapabilityType.Platform, "Windows 10"); caps.SetCapability("deviceName", ""); caps.SetCapability("deviceOrientation", ""); caps.SetCapability("username", "kin"); caps.SetCapability("accessKey", "9cd6-438e-a9635b70953d"); caps.SetCapability("name", TestContext.CurrentContext.Test.Name); return new RemoteWebDriver(new Uri("http://ondemand.saucelabs.com:80/wd/hub"), caps, TimeSpan.FromSeconds(600)); } 函数和自定义代码获取之前的LOOKUPSET总数。

转到End Balance标签中的Report菜单/ Report properties...粘贴此VB功能。

Code

Public Function GetBegBal(values As Object) As String Dim total As Double = 0 For Each value As Double In values total = total + value Next Return total End Function 中使用此表达式:

Beg Balance

=Code.GetBegBal( LOOKUPSET( Fields!Account.Value & "-" & MONTH(Fields!Month.Value)-1, Fields!Account.Value & "-" & MONTH(Fields!Month.Value), Fields!EndBalance.Value, "DataSetName") ) 替换为您的实际名称,然后预览您的报告,它应如下所示:

enter image description here

注意我在Months列中使用DataSetName函数,因此它会根据我的机器中的区域和语言设置(西班牙语)返回月份名称。

如果有帮助,请告诉我。