I have a table with the following details:
-- SQL
CREATE TABLE [dbo].[LedgerTbl](
[LedgerID] [int] NOT NULL,
[Name] [varchar](50) NOT NULL,
[ParentID] [int] NULL,
[Cr Amt] [decimal](8, 2) NOT NULL,
[Dr Amt] [decimal](8, 2) NOT NULL,
CONSTRAINT [PK_LedgerTbl] PRIMARY KEY CLUSTERED
(
[LedgerID] 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
-- Data
INSERT [dbo].[LedgerTbl] ([LedgerID], [Name], [ParentID], [Cr Amt], [Dr Amt]) VALUES (17, N'L1 Ledger', 20, CAST(25252.00 AS Decimal(8, 2)), CAST(0.00 AS Decimal(8, 2)))
INSERT [dbo].[LedgerTbl] ([LedgerID], [Name], [ParentID], [Cr Amt], [Dr Amt]) VALUES (18, N'L2 Ledger', 20, CAST(9000.00 AS Decimal(8, 2)), CAST(0.00 AS Decimal(8, 2)))
INSERT [dbo].[LedgerTbl] ([LedgerID], [Name], [ParentID], [Cr Amt], [Dr Amt]) VALUES (20, N'Master Ledger', NULL, CAST(0.00 AS Decimal(8, 2)), CAST(6900.00 AS Decimal(8, 2)))
INSERT [dbo].[LedgerTbl] ([LedgerID], [Name], [ParentID], [Cr Amt], [Dr Amt]) VALUES (45, N'L1.1 Ledger', 17, CAST(361.00 AS Decimal(8, 2)), CAST(0.00 AS Decimal(8, 2)))
INSERT [dbo].[LedgerTbl] ([LedgerID], [Name], [ParentID], [Cr Amt], [Dr Amt]) VALUES (46, N'L1.1.1 Ledger', 45, CAST(6541.00 AS Decimal(8, 2)), CAST(0.00 AS Decimal(8, 2)))
INSERT [dbo].[LedgerTbl] ([LedgerID], [Name], [ParentID], [Cr Amt], [Dr Amt]) VALUES (47, N'L1.1.2 Ledger', 45, CAST(321.00 AS Decimal(8, 2)), CAST(0.00 AS Decimal(8, 2)))
GO
ALTER TABLE [dbo].[LedgerTbl] WITH CHECK ADD CONSTRAINT [FK_LedgerTbl_LedgerTbl] FOREIGN KEY([ParentID])
REFERENCES [dbo].[LedgerTbl] ([LedgerID])
GO
ALTER TABLE [dbo].[LedgerTbl] CHECK CONSTRAINT [FK_LedgerTbl_LedgerTbl]
GO
The sample data is like this:
LedgerID Name ParentID Cr Amt Dr Amt
20 Master Ledger NULL 0.00 6900.00
17 L1 Ledger 20 25252.00 0.00
18 L2 Ledger 20 9000.00 0.00
45 L1.1 Ledger 17 361.00 0.00
46 L1.1.1 Ledger 45 6541.00 0.00
47 L1.1.2 Ledger 45 321.00 0.00
In the above table for the LedgerID 20, I need all the linked ledgers (in all levels), which are directly or indirectly linked for this ledger. In the above table all ledgers are directly linked to 20.
If I query for balances of ledger 20 it should show like this (adding all the Cr Amt and Dr Amount of all linked ledgers from all levels):
Cr Bal Dr Bal
41475.00 6900.00
Since all the ledgers are directly or indirectly linked to 20, all the Cr Amt and Dr Amt are being summed up.
The results should be like this after adding sum of Cr Amt and Dr Amt of all linked/not linked LedgerID’s:
LedgerID Tot. Cr Amt Tot Dr Amt
20 41475.00 6900.00
18 9000.00 0.00
17 32475.00 0.00
45 7223.00 0.00
46 6541.00 0.00
47 321.00 0.00
Please notice that ledger 18 does not have any child ledgers and hence no need to add any balances.
Please help to achieve this using CTE or any other method. Thanks in advance.
This is what I’ve tried:
;WITH RecursiveLedger(LedgerID, [Name],[Cr Amt], [Dr Amt], LevelNum, LevelIndex, ParentID)
AS (
SELECT lg.LedgerID,
lg.[Name],
lg.[Cr Amt],
lg.[Dr Amt],
1 AS LevelNum,
CAST(lg.LedgerID AS VARCHAR) AS LevelIndex,
lg.ParentID
FROM [LedgerTbl] lg
WHERE lg.ParentID IS NULL
UNION ALL
SELECT l.LedgerID,
l.[Name],
l.[Cr Amt],
l.[Dr Amt],
r.LevelNum + 1 AS LevelNum,
CAST(r.LevelIndex + '.' + CAST(ROW_NUMBER() OVER(ORDER BY l.ParentID) AS VARCHAR) AS VARCHAR) AS LevelIndex,
l.ParentID
FROM [LedgerTbl] l,
RecursiveLedger r
WHERE r.LedgerID = l.ParentID
) SELECT * FROM RecursiveLedger
1 Answer