本文介绍了如何获得父母的总和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我们有两张桌子。使用这些表我们可以得到这个结果。使用单选查询。 < pre>账户表 AccountID名称父母 1标头100空白 2标头110 1 3儿童111 2 4儿童112 2 5儿童113 2 6儿童120 1 7标题130 1 8儿童131 7 9儿童132 7 10标题200 NULL 11标题210 10 12儿童221 10 13儿童220 10 14标题300空白 15儿童310 14 AccountLedger表账户价值 3 50.00 3 10.00 3 50.00 3 80.00 3 40.00 4 20.00 4 10.00 4 100.00 5 80.00 5 90.00 5 60.00 6 100.00 8 40.00 8 70.00 8 10.00 8 40.00 9 20.00 12 60.00 12 30.00 12 70.00 13 10.00 13 100.00 15 30.00 15 90.00 15 40.00 需要输出: - AccountId父名称值 1 NULL标题100 870.00 2 1标题110 590.00 3 2儿童111 230.00 4 2儿童112 130.00 5 2儿童113 2 30.00 6 1儿童120 100.00 7 1标题130 180.00 8 7儿童131 160.00 9 7儿童132 20.00 10 NULL标题200 270.00 11 10标题210 0.00 12 10儿童221 160.00 13 10儿童220 110.00 14空标题300 160.00 15 14儿童310 160.00 我尝试过: ;随着CTE1 AS (选择AccountId,AccountName,ParentId,null作为Accountvalue来自AccountList Union All 选择A.AccountId,A.AccountName,A.ParentId, SUM(ISNULL(Accountvalue,0))来自AccountList A 内部加入Accountledger T On A.AccountId = T.Account GROUP BY AccountId,AccountName,ParentID )选择Accountid,AccountName,ParentId ,SUM(ISNULL(Accountvalue,0))as AccountValue 来自CTE1 Group By Accountid,AccountName,ParentId ORDER BY AccountId - option(maxrecursion 0) 解决方案 we have two tables. and using these table can we get this result.Using single select query.<pre>Account TableAccountIDNameParent1Header 100NULL2Header 11013Child 11124Child 11225Child 11326Child 12017Header 13018Child 13179Child 132710Header 200NULL11Header 2101012Child 2211013Child 2201014Header 300NULL15Child 31014AccountLedger TableAccountValue350.00310.00350.00380.00340.00420.00410.004100.00580.00590.00560.006100.00840.00870.00810.00840.00920.001260.001230.001270.001310.0013100.001530.001590.001540.00OUTPUT NEEDED :-AccountIdParentNamevalue1NULLHeader 100870.0021Header 110590.0032Child 111230.0042Child 112130.0052Child 113230.0061Child 120100.0071Header 130180.0087Child 131160.0097Child 13220.0010NULLHeader 200270.001110Header 2100.001210Child 221160.001310Child 220110.0014NULLHeader 300160.001514Child 310160.00What I have tried:;With CTE1AS(Select AccountId,AccountName, ParentId,null as Accountvalue From AccountListUnion AllSelect A.AccountId, A.AccountName, A.ParentId,SUM(ISNULL(Accountvalue,0)) From AccountList AInner Join Accountledger T On A.AccountId = T.Account GROUP BY AccountId,AccountName,ParentID)Select Accountid, AccountName ,ParentId,SUM(ISNULL(Accountvalue,0)) as AccountValueFrom CTE1Group By Accountid,AccountName, ParentIdORDER BY AccountId--option (maxrecursion 0) 解决方案 这篇关于如何获得父母的总和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-25 02:43