Quantcast
Viewing latest article 20
Browse Latest Browse All 207

Recursively sum child's nominal to parent, on dynamic level parent-child structure

i have data look like this :

Image may be NSFW.
Clik here to view.
pic1

i try to sum the Nominal column to each respective parent, so it will looks like this :

Image may be NSFW.
Clik here to view.
pic2

i provided the sql script for the data :

SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE TABLE [dbo].[Account](    [AccountSeq] [bigint] IDENTITY(1,1) NOT NULL,    [ParentAccountSeq] [bigint] NULL,    [AccountCode] [nvarchar](50) NOT NULL,    [AccountName] [nvarchar](50) NOT NULL,    [Nominal] [decimal](18, 2) NOT NULL) ON [PRIMARY]GOSET IDENTITY_INSERT [dbo].[Account] ON GOINSERT [dbo].[Account] ([AccountSeq], [ParentAccountSeq], [AccountCode], [AccountName], [Nominal]) VALUES (1, NULL, N'101', N'AKTIVA LANCAR', CAST(0.00 AS Decimal(18, 2)))GOINSERT [dbo].[Account] ([AccountSeq], [ParentAccountSeq], [AccountCode], [AccountName], [Nominal]) VALUES (2, 1, N'10101', N'KAS', CAST(0.00 AS Decimal(18, 2)))GOINSERT [dbo].[Account] ([AccountSeq], [ParentAccountSeq], [AccountCode], [AccountName], [Nominal]) VALUES (3, 2, N'1010101', N'KAS KECIL (IDR)', CAST(10000.00 AS Decimal(18, 2)))GOINSERT [dbo].[Account] ([AccountSeq], [ParentAccountSeq], [AccountCode], [AccountName], [Nominal]) VALUES (4, 2, N'1010102', N'KAS KECIL ($$$)', CAST(15000.00 AS Decimal(18, 2)))GOINSERT [dbo].[Account] ([AccountSeq], [ParentAccountSeq], [AccountCode], [AccountName], [Nominal]) VALUES (5, 1, N'10102', N'BANK', CAST(0.00 AS Decimal(18, 2)))GOINSERT [dbo].[Account] ([AccountSeq], [ParentAccountSeq], [AccountCode], [AccountName], [Nominal]) VALUES (6, 5, N'1010201', N'BCA PKU AC: 220.391', CAST(20000.00 AS Decimal(18, 2)))GOINSERT [dbo].[Account] ([AccountSeq], [ParentAccountSeq], [AccountCode], [AccountName], [Nominal]) VALUES (7, 5, N'1010202', N'BCA PKU AC: 220.279', CAST(25000.00 AS Decimal(18, 2)))GOINSERT [dbo].[Account] ([AccountSeq], [ParentAccountSeq], [AccountCode], [AccountName], [Nominal]) VALUES (8, 1, N'10104', N'PIUTANG USAHA', CAST(30000.00 AS Decimal(18, 2)))GOINSERT [dbo].[Account] ([AccountSeq], [ParentAccountSeq], [AccountCode], [AccountName], [Nominal]) VALUES (10, 1, N'10105', N'PIUTANG PROYEK', CAST(40000.00 AS Decimal(18, 2)))GOINSERT [dbo].[Account] ([AccountSeq], [ParentAccountSeq], [AccountCode], [AccountName], [Nominal]) VALUES (11, NULL, N'201', N'HUTANG JANGKA PENDEK', CAST(50000.00 AS Decimal(18, 2)))GOINSERT [dbo].[Account] ([AccountSeq], [ParentAccountSeq], [AccountCode], [AccountName], [Nominal]) VALUES (12, NULL, N'301', N'MODAL', CAST(60000.00 AS Decimal(18, 2)))GOSET IDENTITY_INSERT [dbo].[Account] OFFGO

the last sql query i tried :

WITH cteTest AS (    SELECT    AccountSeq,    ParentAccountSeq,    AccountCode,    AccountName,    CAST(Nominal AS DECIMAL(18,2)) AS Nominal    FROM    [Account2]    WHERE    ParentAccountSeq IS NULL    UNION ALL    SELECT    a.AccountSeq,    a.ParentAccountSeq,    a.AccountCode,    a.AccountName,    CAST((a.Nominal + cte.Nominal) AS DECIMAL(18,2)) AS Nominal    FROM    [Account2] a    INNER JOIN [cteTest] cte ON cte.AccountSeq = a.ParentAccountSeq)SELECT*FROM[cteTest]ORDER BYAccountSeq

Please can anyone help me to solve this?


Viewing latest article 20
Browse Latest Browse All 207

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>