0

我有这个 SQL 查询,它搜索父母和孩子并计算权重。

select componentid,
         (select sum(cs.weightkg)
            from component c2, componentstats cs
           where c2.componentstatsid = cs.componentstatsid
               start with c2.componentid = c1.componentid
         connect by prior c2.componentid = c2.fkcomponentid) sum_weightkg
from component c1
start with c1.fkcomponentid = 100
connect by prior componentid = fkcomponentid

问题是我希望 SQL 查询将总权重作为数字返回(它发现要聚合的所有数字)。

这是我得到的结果:

COMPONENTID            SUM_WEIGHTKG           
---------------------- ---------------------- 
201                    410                    
231                    210                    
323                    10

你能帮我重写 SQL 查询吗?

http://www.sqlfiddle.com/#!4/def0e/2

4

1 回答 1

1
select max(sum_weightkg) from
(select componentid,
             (select sum(cs.weightkg)
                from component c2, componentstats cs
               where c2.componentstatsid = cs.componentstatsid
                   start with c2.componentid = c1.componentid
             connect by prior c2.componentid = c2.fkcomponentid) sum_weightkg
    from component c1
   start with c1.fkcomponentid = 100
   connect by prior componentid = fkcomponentid);

我不确定这是最好的方法,但它应该有效。

于 2013-01-30T15:31:07.847 回答