Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.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_Sql Server_Sql Server 2008 - Fatal编程技术网

Sql 更新同一表中记录的总和(将多条记录减少为一条)

Sql 更新同一表中记录的总和(将多条记录减少为一条),sql,sql-server,sql-server-2008,Sql,Sql Server,Sql Server 2008,我们有多个具有相同键(项)的记录,我们希望使用更新将这些记录的总和减少为一个记录。目标记录就是这些记录之一。我们也希望在多个项目上这样做。 示例(): 我已经试过了: update Itemstock Set queuequantity = (Select Sum(IS_2.queuequantity) from itemstock IS_2 where (IS_2.item=item) and IS_2.bookyear=2017), rese

我们有多个具有相同键(项)的记录,我们希望使用更新将这些记录的总和减少为一个记录。目标记录就是这些记录之一。我们也希望在多个项目上这样做。 示例():

我已经试过了:

update Itemstock 
    Set queuequantity       = (Select Sum(IS_2.queuequantity)       from itemstock IS_2 where (IS_2.item=item) and IS_2.bookyear=2017),
        reservationquantity = (Select Sum(IS_2.reservationquantity) from itemstock IS_2 where (IS_2.item=item) and IS_2.bookyear=2017),
        warehousequantity   = (Select Sum(IS_2.warehousequantity)   from itemstock IS_2 where (IS_2.item=item) and IS_2.bookyear=2017),
        averageout          = (Select Sum(IS_2.averageout)          from itemstock IS_2 where (IS_2.item=item) and bookyear=2017),
        purchasequantity    = (Select Sum(IS_2.purchasequantity)    from itemstock IS_2 where (IS_2.item=item) and IS_2.bookyear=2017),
        financialamount     = (Select Sum(IS_2.financialamount)     from itemstock IS_2 where (IS_2.item=item) and IS_2.bookyear=2017),
        financialresult     = (Select Sum(IS_2.financialresult)     from itemstock IS_2 where (IS_2.item=item) and IS_2.bookyear=2017),
        technicalquantity   = (Select Sum(IS_2.technicalquantity)   from itemstock IS_2 where (IS_2.item=item) and IS_2.bookyear=2017),
        technicalamount     = (Select Sum(IS_2.technicalamount)     from itemstock IS_2 where (IS_2.item=item) and IS_2.bookyear=2017),
        technicalresult     = (Select Sum(IS_2.technicalresult)     from itemstock IS_2 where (IS_2.item=item) and IS_2.bookyear=2017)
    where item=(select objectid from item where Itemgroup=15900239)
        and Stockno=0
        and bookyear=2017`
但是,这会产生以下错误:

单件选择中的多行


我明白原因,但无法找到解决方案。我不是SQL专家

不要使用所有相关的子查询,尝试使用聚合一次,并在连接时进行更新。此外,在where子句中使用
,因为子查询可能返回多行:

update a
set a.queuequantity = b.queuequantity,
    a.reservationquantity = b.reservationquantity,
    ...
from itemstock a
join (
    select item,
        Sum(queuequantity) as queuequantity,
        Sum(reservationquantity) as reservationquantity,
        ...
    from itemstock
    where bookyear = 2017
    group by item
    ) b on a.item = b.item
where a.item in (
        select objectid
        from item
        where Itemgroup = 15900239
        )
    and a.Stockno = 0
    and a.bookyear = 2017

不要使用所有相关的子查询,尝试使用聚合一次,并在连接时进行更新。此外,在where子句中使用
,因为子查询可能返回多行:

update a
set a.queuequantity = b.queuequantity,
    a.reservationquantity = b.reservationquantity,
    ...
from itemstock a
join (
    select item,
        Sum(queuequantity) as queuequantity,
        Sum(reservationquantity) as reservationquantity,
        ...
    from itemstock
    where bookyear = 2017
    group by item
    ) b on a.item = b.item
where a.item in (
        select objectid
        from item
        where Itemgroup = 15900239
        )
    and a.Stockno = 0
    and a.bookyear = 2017

您可以连接到项表而不是使用where语句。您可以连接到项表而不是使用where语句。