Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8253201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:47:51+00:00 2026-06-08T00:47:51+00:00

I have a table with the following details: — SQL CREATE TABLE [dbo].[LedgerTbl]( [LedgerID]

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T00:47:52+00:00Added an answer on June 8, 2026 at 12:47 am
    with anc as 
    (
        select ledgerid, parentid
        from [LedgerTbl] where parentid is not null
        union all
        select c.ledgerid, isnull(anc.parentid, anc.parentid)
        from anc 
            inner join [LedgerTbl] c on anc.ledgerid = c.parentid
    ), anc2 as
    (
        select * from anc
        union all
        select ledgerid, ledgerid
        from ledgertbl 
    )
    select  a.parentid,
            sum(l.[Cr Amt]), 
            sum(l.[dr Amt]) 
    from anc2 a
        inner join ledgertbl l on a.ledgerid = l.ledgerid
    group by a.parentid
    order by a.parentid;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have table with following details Table name EMPLOYEE and columns EMPID (PK smallint
I have the following table structure: EVENT_ID(INT) EVENT_NAME(VARCHAR) EVENT_DATE(DATETIME) EVENT_OWNER(INT) I need to add
I am fairly new to Indexes. I have table following table [FORUM1] [msg_id] [int]
I have table with following structure and records. table t1 +------+-------------+---------------------+ | name |
Imagine I have following table: NAME DATE OTHER_CONTANT 'A' '2012-06-05' 'baz' 'A' '2012-06-04' 'bar'
i have a details table with columns: user_id int code int value int And
I have a table with details on personnel. I would like to create a
I have a table containing the following details: date_info info 2009-06-23 1 2009-06-24 2
I have a table T1 with a column details , data column has following
Suppose I have the following table definition: CREATE TABLE x (i serial primary key,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.