有一表
编号       数量
0002       66
0001       60
0005       110
0001       90
0002       80
0003       90
想从上表中得到下面编号表
0001
0001
0001
0002
0002
0003
0005
0005算法:
把每个编号数量相加除以50取整数即为该编号的条数。
如编号0001数量和为150,除以50取整数得3,故0001有三行。
谢谢各位!!

解决方案 »

  1.   

    用游标吧.declare c1 cursor for select bh,sum(sl) sl from table group by bh
    open c1
    while(1=1)
    begin
    fetch c1 into @bh,@sl
    if @@fetch_status <> 0
    break
    if @sl >=50
    begin
    insert into @temp(bh) values (@bh)
    @sl = @sl -50
    end
    close c1
    deallocate c1
    占位问个小问题  ..有一值如果a为0的话想让他取Null值,应该怎么写,例:
    isnull(case a when 0 then null else a end,b)   --这语法不对,怎么改改
      

  2.   

    --> 测试数据: #tb
    if object_id('tb') is not null drop table tb
    go
    create table tb (bh varchar(4),sl int)
    insert into tb
    select '0002',66 union all
    select '0001',60 union all
    select '0005',110 union all
    select '0001',90 union all
    select '0002',80 union all
    select '0003',90----------------------------------
    if object_id('tb1') is not null drop table tb1
    go
    create table tb1 (bh varchar(4))
    declare @bh varchar(10), @sl intdeclare cur cursor for select bh,sl=sum(sl) from tb group by bhopen cur 
    fetch next from cur into @bh,@sl
    while @@fetch_status=0
    begin
    declare @n int
    set @n=1 while @n<=(@sl/50)
    begin
    insert tb1 select @bh
    set @n=@n+1
    end fetch next from cur into @bh,@sl
    end
    go 
    select * from  tb1 
    bh
    ----
    0001
    0001
    0001
    0002
    0002
    0003
    0005
    0005(8 行受影响)
      

  3.   

    -- =============================================
    -- Author:      T.O.P
    -- Create date: 2009/11/27
    -- Version:     SQL SERVER 2005
    -- =============================================
    declare @tb table([a] varchar(4),[b] int)
    insert @tb
    select '0002',66 union all
    select '0001',60 union all
    select '0005',110 union all
    select '0001',90 union all
    select '0002',80 union all
    select '0003',90select [a],sum([b])/50 as b into #t from @tb group by [a] ;with cte2 as
    (
    select * from #t
    union all
    select b.a, b.b-1 as b 
    from #t a inner join cte2 b on a.[a] = b.[a]
    where b.b>1 
    )
    select a from cte2 order by adrop table #t
    --测试结果:
    /*
    a
    ----
    0001
    0001
    0001
    0002
    0002
    0003
    0005
    0005(8 row(s) affected)
    */
      

  4.   


    case a=0 then null end
      

  5.   


    case when a=0 then null else a end
      

  6.   

    ---测试数据---
    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([number] varchar(4),[qty] int)
    insert [tb]
    select '0002',66 union all
    select '0001',60 union all
    select '0005',110 union all
    select '0001',90 union all
    select '0002',80 union all
    select '0003',90
     
    ---查询---
    select a.number
    from
    (
    select number,sum(qty)/50 as cnt
    from tb
    group by number
    ) a
    join master..spt_values b on b.type='P' and b.number between 1 and a.cnt---结果---
    number 
    ------ 
    0001
    0001
    0001
    0002
    0002
    0003
    0005
    0005(所影响的行数为 8 行)
      

  7.   

    -------------------------------------
    --  Author : liangCK 梁爱兰
    --  Comment: 小梁 爱 兰儿
    --  Date   : 2009-11-27 15:31:35
    -------------------------------------
     
    --> 生成测试数据: @tb
    DECLARE @tb TABLE (编号 varchar(4),数量 int)
    INSERT INTO @tb
    SELECT '0002',66 UNION ALL
    SELECT '0001',60 UNION ALL
    SELECT '0005',110 UNION ALL
    SELECT '0001',90 UNION ALL
    SELECT '0002',80 UNION ALL
    SELECT '0003',90--SQL查询如下:SELECT A.编号
    FROM (SELECT 编号,SUM(数量)/50 AS flag FROM @tb GROUP BY 编号) AS A
        JOIN master.dbo.spt_values AS B
    ON B.type = 'P' AND number < flag
      

  8.   

    --> 测试数据:@table
    declare @table table([编号] varchar(4),[数量] dec(9,2))
    insert @table
    select '0002',66 union all
    select '0001',60 union all
    select '0005',110 union all
    select '0001',90 union all
    select '0002',80 union all
    select '0003',90
    select r.编号
    from (select 编号,cast(sum(数量)/50 as int) as 数量 from @table group by 编号) r
        JOIN master.dbo.spt_values as t
    on t.type = 'P' and number < 数量
    --结果
    ------------------------
    0001
    0001
    0001
    0002
    0002
    0003
    0005
    0005