order 表如下
ordno sku_id batch attr1 custom quant (其中sku_id,batch,attr1,custom)是条件分组
0001 sku001 01 1 cu01 10
0002 sku001 01 1 cu01 20
0003 sku001 02 1 cu03 14
0004 sku002 01 2 cu05 10
0004 sku002 02 1 cu01 4
想得到这样的结果
ordno sku_id batch attr1 custom quant 总数
0001 sku001 01 1 cu01 10 30--如果相同总数
0002 sku001 01 1 cu01 20 30--如果相同总数
0003 sku001 02 1 cu03 14 14
0004 sku002 01 2 cu05 10 10
0005 sku002 02 1 cu04 4 4
0006 sku002 02 1 cu01 4 8--如果相同总数
0007 sku002 02 1 cu01 4 8--如果相同总数

解决方案 »

  1.   

    select t.* , quant = (select sum(quant) from [order] where sku_id = t.sku_id and batch = t.batch and attr1 = t.attr1 and custom = t.custom) from [order] t
      

  2.   


    --给出的结果是不是有问题呀?orderno 005 006 从哪来的呢?
    select ordno, sku_id, batch, attr1, custom, quant,
           总数 = (select sum(quant) from order where sku_id =a.sku_id and batch = a.batch
                        and attr1 = a.attr1 and custom = a.custom)
    from order a 
      

  3.   

    select a.*,b.quant from order a left join (select ordno,quant=sum(quant) from order group by sku_id,batch,attr1,custom) b on a.ordno=b.ordno
      

  4.   

    sql 20000
    select 
    t.* , 
    quant=
    (
    select sum(quant) 
    from [order] 
    where sku_id = t.sku_id and batch = t.batch and attr1 = t.attr1 and custom = t.custom) from [order] t