如何在 SSRS 中获取某行的前一个值
How to get the previous value of a row in SSRS
我的 SSRS 报告有两个组(帐户、月份)。 Account 是 Parent,Month 是子组。现在我想将每个月的期末余额显示为下个月的期初余额。下面给出了示例报告。红色字体表示每组的总和。请记住,我试图通过使用 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() 函数后
您可以在数据集的 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")
)
您可以使用 LOOKUPSET
函数和自定义代码来获取之前的 End Balance
总数。
转到 Report
菜单/Code
选项卡中的 Report properties...
粘贴此 VB 函数。
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")
)
将 DataSetName
替换为您的真实姓名,然后预览您的报告,它应该如下所示:
请注意,我在“月份”列中使用了 MONTHNAME
函数,因此它 returns 月份名称基于我机器中的区域和语言设置(西班牙语)。
如果有帮助请告诉我。
我的 SSRS 报告有两个组(帐户、月份)。 Account 是 Parent,Month 是子组。现在我想将每个月的期末余额显示为下个月的期初余额。下面给出了示例报告。红色字体表示每组的总和。请记住,我试图通过使用 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() 函数后
您可以在数据集的 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")
)
您可以使用 LOOKUPSET
函数和自定义代码来获取之前的 End Balance
总数。
转到 Report
菜单/Code
选项卡中的 Report properties...
粘贴此 VB 函数。
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")
)
将 DataSetName
替换为您的真实姓名,然后预览您的报告,它应该如下所示:
请注意,我在“月份”列中使用了 MONTHNAME
函数,因此它 returns 月份名称基于我机器中的区域和语言设置(西班牙语)。
如果有帮助请告诉我。