Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 试图计算重量之和_Sql_Oracle - Fatal编程技术网

Sql 试图计算重量之和

Sql 试图计算重量之和,sql,oracle,Sql,Oracle,我返回四列多行值 并且需要其中一个列值的聚合 我返回四列多行值 列:weight,a,b,c,d它包含18行值,现在我想从中得到一列weight的聚合,即sum(weight) 预期值:权重列中所有值的总和。 实际值:仅获取列中的权重值如何从两个表联合的结果中进行聚合不知道。您可以在下面进行尝试- select sum(weight) as totalweight from ( select a.prod_weight as weight,a,b,c,d from tbl1 union all

我返回四列多行值 并且需要其中一个列值的聚合

我返回四列多行值 列:weight,a,b,c,d它包含18行值,现在我想从中得到一列weight的聚合,即sum(weight)

预期值:权重列中所有值的总和。 实际值:仅获取列中的权重值如何从两个表联合的结果中进行聚合不知道。

您可以在下面进行尝试-

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 
你可以试试这个

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
或者,如果只想在最后一列中添加sum,则不需要列:-

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 

我猜您希望在这里使用UNION ALL而不是UNION。查询很好,但我只需要单列totalweight,它将weight列中每行值的总和作为单个TotalWeights。除非您希望产生删除重复项的开销,否则不要使用
UNION
。如果两个表具有相同的值,则将返回不正确的结果。
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