客户传过来的订单数据,根据数量自动拆分成 N 份,每份数量最多 1000
相同的订单 orderid 相同, itemid 不一样如下:orderid itemid quantity ttquantity1001 1001-1 1000 -1
1001 1001-2 1000 -1
1001 1001-3 1000 -11002 1002-1 1000 -1
1002 1002-2 500 -1现在要把相同 orderid 的数量合计,放在第一份 ttquantity 里,其他 ttquantity 设为 0,即要变成:orderid itemid quantity ttquantity1001 1001-1 1000 3000
1001 1001-2 1000 0
1001 1001-3 1000 01002 1002-1 1000 1500
1002 1002-2 500 0现在已有的数据有几十万,而每次传过来的数据有5K左右,有什么高效的更新方法?谢谢!

解决方案 »

  1.   

    update 订单数据 a left join (
    select orderid,min(itemid) as itemid, sum(quantity) as ttquantity
    from  订单数据
    Group by   orderid
    ) b on a.orderid=  b.orderid and a.itemid  =b.itemid
    Set a.ttquantity =ifnull (b.ttquantity,0)
      

  2.   

    update 订单数据 a left join (
    select orderid,min(itemid) as mi, sum(quantity) as squantity
    from  订单数据 Group by orderid) b on a.orderid= b.orderid and a.itemid  =b.mi
    Set a.ttquantity =COALESCE(squantity,0)
      

  3.   

    因为数据不是一次性进来的,需要多次执行楼上sql的话,需要考虑效率,过滤以前已经做过的update 订单数据 a left join (
    select orderid,min(itemid) as itemid, sum(quantity) as ttquantity
    from  订单数据
    where ttquantity=-1
    Group by   orderid
    ) b on a.orderid=  b.orderid and a.itemid  =b.itemid
    Set a.ttquantity =ifnull (b.ttquantity,0)
    where a.ttquantity=-1
      

  4.   

    upcmill: 
    是的,之所以我把 ttquantity 默认设为 -1
    就是为避免重复处理