试图计算重量之和

时间:2019-08-07 07:06:57

标签: sql oracle

我要返回具有多行值的四列 并希望汇总其中一个列值。

我要返回具有多行值的四列 列:weight,a,b,c,d它包含十八行值,现在我希望从中汇总列权重之一。即sum(weight)

select  a.prod_weight weight,a,b,c,d from tbl1
union
select  a.prod_weight weight,a,b,c,d from tbl2

预期:重量列中所有值的总和。 当前:仅从列中获取权重值,如何从两个表联合的结果中汇总权重值就不知道了。

2 个答案:

答案 0 :(得分:1)

您可以在下面尝试-

select sum(weight) as totalweight
from
(
select  a.prod_weight as weight,a,b,c,d from tbl1
union all
select  a.prod_weight,a,b,c,d from tbl2
)A 

答案 1 :(得分:0)

您可以尝试这个。

Select sum(isnull(weight,0)) as Weight, a, b, c, d from (
select  a.prod_weight weight, a, b, c, d from tbl1
union all
select  a.prod_weight weight, a, b, c, d from tbl2
) as d 
group by a, b, c, d

或者,如果您只想在最后一次添加总和,则在这种情况下,您不需要列:-

select  a.prod_weight weight,a,b,c,d from tbl1
union all
select  a.prod_weight weight,a,b,c,d from tbl2
union all
Select sum(isnull(weight,0)) as Weight, null, null, null, null from (
select  a.prod_weight weight,a,b,c,d from tbl1
union all
select  a.prod_weight weight,a,b,c,d from tbl2
) as d