我的表
create table tbl_trial_balance
(
ID int primary key,
Name char(20) not null,
Parent_code int references tbl_trial_balance(ID), -- Self Join
Debit float,
Credit float
)
go
INSERT INTO tbl_trial_balance (ID,Name,Parent_code,Debit,Credit) values(1,'Assets',null,null,null);
INSERT INTO tbl_trial_balance (ID,Name,Parent_code,Debit,Credit) values(2,'Current Assets',1,null,null);
INSERT INTO tbl_trial_balance (ID,Name,Parent_code,Debit,Credit) values(3,'Bank Account',2,null,null);
INSERT INTO tbl_trial_balance (ID,Name,Parent_code,Debit,Credit) values(4,'DBBL A/C',3,5000,null);
INSERT INTO tbl_trial_balance (ID,Name,Parent_code,Debit,Credit) values(5,'DBBL A/C',3,4000,null);
INSERT INTO tbl_trial_balance (ID,Name,Parent_code,Debit,Credit) values(6,'DBBL A/C',3,3000,null);
INSERT INTO tbl_trial_balance (ID,Name,Parent_code,Debit,Credit) values(7,'DBBL A/C',3,null,7000);
INSERT INTO tbl_trial_balance (ID,Name,Parent_code,Debit,Credit) values(8,'IFIC A/C',3,3000,null);
INSERT INTO tbl_trial_balance (ID,Name,Parent_code,Debit,Credit) values(9,'IFIC A/C',3,5000,null);
INSERT INTO tbl_trial_balance (ID,Name,Parent_code,Debit,Credit) values(10,'IFIC A/C',3,null,6000);
INSERT INTO tbl_trial_balance (ID,Name,Parent_code,Debit,Credit) values(11,'IFIC A/C',3,null,9000);
INSERT INTO tbl_trial_balance (ID,Name,Parent_code,Debit,Credit) values(112,'IFIC A/C',3,null,8000);
go
我想对特定节点的所有子节点的值求和。例如,对于我期望的顶级Assets
音符:
Name Debit Credit
Assets 20000 30000
对于第二级Current Asset
节点,我希望:
Name Debit Credit
Current Asset 20000 30000
我不能用简单的GROUP BY来做到这一点:
select
Name,
SUM(Debit),
SUM(Credit)
FROM tbl_trial_balance
GROUP BY Name
如何做到这一点?
答案 0 :(得分:0)
好的,如果所需的输出 是一个拼写错误,那么你可以使用它:
class LibFormatter(ManagedFile):
def simple_parameter(self, param, value):
self.file.write('{}: {}\n'.format(param, value))
with LibFormatter('library.txt', 'w') as lib:
... # etc