1

我有一张包含以下详细信息的表格:

-- 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

样本数据如下:

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 

在 LedgerID 20 的上表中,我需要所有链接的分类帐(在所有级别),这些分类帐直接或间接链接到此分类帐。在上表中,所有分类帐都直接链接到 20。
如果我查询分类帐 20 的余额,它应该显示如下(添加所有级别的所有链接分类帐的所有 Cr Amt 和 Dr Amount):

Cr Bal        Dr Bal
41475.00    6900.00

由于所有分类帐都直接或间接与 20 相关联,因此正在汇总所有 Cr Amt 和 Dr Amt。
添加所有链接/未链接 LedgerID 的 Cr Amt 和 Dr Amt 的总和后,结果应该是这样的:

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

请注意,账本 18 没有任何子账本,因此无需添加任何余额。

请帮助使用 CTE 或任何其他方法实现此目的。提前致谢。

这是我尝试过的:

;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
4

1 回答 1

1
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;
于 2012-07-19T15:23:02.507 回答