2016-10-06 8 views
0

SSRSレポートには、2つのグループ(アカウント、月)があります。アカウントは親です。月は子グループです。今度は、各月の期末残高を次月の初めの残高として表示したいと考えています。サンプルレポートは次のとおりです。赤いフォントは各グループのSUMを示します。 SSRS Previous()関数を使用して結果を取得しようとしていますが、期待される結果を得ることはできません。SSRSの行の前の値を取得する方法

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 

前()関数を使用した後 enter image description here

+0

サンプルデータを追加できますか?私はそれが達成できると思います。 –

答えて

1

End Balance合計を取得するためにLOOKUPSET機能やカスタムコードを使用することができます。

このVB機能を貼り付けたCodeタブのReportメニュー/ Report properties...に移動します。 Beg Balance使用で

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 

この式:

=Code.GetBegBal(
LOOKUPSET(
Fields!Account.Value & "-" & MONTH(Fields!Month.Value)-1, 
Fields!Account.Value & "-" & MONTH(Fields!Month.Value), 
Fields!EndBalance.Value, 
"DataSetName") 
) 

はあなたの実際の名前でDataSetNameを交換し、その後、レポートをプレビュー、それは次のようになります。

enter image description here

注月の列にMONTHNAME関数を使用していますので、それに基づいて月の名前を返します私のマシンの地域と言語の設定(スペイン語)。

これが役立つかどうか教えてください。

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") 
) 
+0

LAG()はSQLサーバー2008 R2でサポートされていません –

+0

SSRS式で解答を更新しました。また、ROW_NUMBER()を使用したT-SQLの置き換え。 – p2k

+0

あなたのSSRS表現コードをありがとう。私は実際にこれを探していた。それは私のために完全に働いています。 –

関連する問題