select size,qty=case when size='m' then qty when size='s' then qty when size='L' then sum(qty)/(select count(size) from table1 where size='L') end from table1
或者
select size,qty from table1 where size='m'
union all
select size,qty from table1 where size='s'
union all
select size,qty=sum(qty)/(select count(size) from table1 where size='L') from table1 where size='L'

解决方案 »

  1.   

    netcup兄, 这样不行, 要是有很多笔数据?
      

  2.   

    对呀,你得把业务规则告诉我们啊,当SIZE=什么是拆分成什么,什么情况不拆分,分类除了M\S\L还有什么?
      

  3.   

    if object_id('t_chai') is not null
       drop table t_chai
    if object_id('dbo.f_rlchai') is not null
       drop function dbo.f_rlchai
    go
    create table t_chai(SIZE_ch varchar(10),QTY int)
    insert t_chai
    select 'M',400 union all
    select 'S',500 union all
    select 'L',1500
    select * from  t_chai
    go
    create function dbo.f_rlchai(@size varchar(10))
    returns @t table (SIZE_ch varchar(10),QTY int)
    as
    begin
       declare @qty int,@qtynow int
       select @qty=QTY from t_chai where SIZE_ch=@size
       while @qty>500
       begin
          set @qtynow=500
          insert @t select @size,@qtynow
          set @qty=@qty-500
       end
       insert @t select @size,@qty
       return 
    end
    go
    declare @tt table(SIZE_ch varchar(10),QTY int)
    select dbo.f_chai(SIZE_ch) into @tt from  t_chai where QTY>500)
    select * from t_chai where QTY<=500
    union all
    select * from @tt
      

  4.   

    业务规则是: 当QTY>500, 则进行拆分, SIZE是主键, Select   size,qty   from   table1 
    SIZE QTY 
    M    400
    S    500
    L    1500 If   qty> 500,   则进行拆分,   要得如下结果,   求最简单写法(能否不用循环) 
    SIZE QTY 
    M    400 
    S    500 
    L    500 
    L    500 
    L    500 我还在调试JL99000兄的
      

  5.   

    select   size,qty=case   when   size='m'   then   qty   when   size='s'   then   qty   when   size='L'   then   sum(qty)/500   end   from   table1
    这样不就可以了?如果你的SIZE很多的话,可以用动态行列转换