Create table #
(
ID tinyint,
[Name] varchar(50),
[类别] varchar(50),
[最大数量] tinyint,
[已放数量] tinyint,
[剩余数量] tinyint
);
insert into #(ID,[Name],[类别],[最大数量],[已放数量],[剩余数量])
select * from (
select 1 as ID,'仓库1' as [Name],'电器类' as [类别],35 as [最大数量],25 as [已放数量],10 as [剩余数量] union all
select 2,'仓库2','电器类',30,28,2 union all
select 3,'仓库3','电器类',25,21,4 union all
select 4,'仓库4','电器类',35,25,10 union all
select 5,'仓库5','电器类',35,19,16 
)a;select * from # ;
drop table # ;--测试数据:ID [Name] 类别 最大数量 已放数量 剩余数量
1 仓库1 电器类 35 25 10
2 仓库2 电器类 30 28 2
3 仓库3 电器类 25 21 4
4 仓库4 电器类 35 32 3
5 仓库5 电器类 35 19 16--想达到的效果(此例中应该存在多种拆分方式)
方式一:拆分【仓库5】,将【10个】放入【仓库1】;将【2】放入【仓库2】;将【4】放入【仓库3】;将【3】放入【仓库4】。--需求描述:
如果我现在想整理1-5的仓库,看看是否存在将某一个仓库的【已放数量】『完全』拆分到【其他的】仓库中。拆分完成后,被拆分的仓库的【已放数量】为【0】
--目前问题:
1、我怎么【判断】是否可以完全拆分到其他仓库(可能存在无法拆分的情况)
2、拆分的合理性,【尽量】保证拆分后,拆分到的仓库的【剩余数量】为【0】,并且确保被拆分仓库的【已放数量】为【0】

解决方案 »

  1.   

    1、我怎么【判断】是否可以完全拆分到其他仓库(可能存在无法拆分的情况)
    if (select sum(剩余数量) from # where id <> 5) >= (select 已放数量  from # where id = 5)
        print '可以拆分'
    else
        print '不可以拆分'2、拆分的合理性,【尽量】保证拆分后,拆分到的仓库的【剩余数量】为【0】,并且确保被拆分仓库的【已放数量】为【0】这个需要有个算法,保证哪个有限,有空的时候可以写写
     
     
     
      

  2.   

    如果只看看是否存在将某一个仓库是否可以拆分--> 测试数据: #t
    if object_id('tempdb.dbo.#t') is not null drop table #t
    create table #t (ID int,[Name] varchar(5),Category varchar(6),MaxQty int,CurQty int,RestQty int)
    insert into #t
    select 1,'仓库1','电器类',35,25,10 union all
    select 2,'仓库2','电器类',30,28,2 union all
    select 3,'仓库3','电器类',25,21,4 union all
    select 4,'仓库4','电器类',35,32,3 union all
    select 5,'仓库5','电器类',35,19,16select * from #tselect * from #t  where MaxQty =(select  SUM(maxqty)-SUM(curqty)  from #t)
    ID Name Category MaxQty CurQty RestQty
    1 仓库1 电器类 35 25 10
    4 仓库4 电器类 35 32 3
    5 仓库5 电器类 35 19 16
      

  3.   


    其实实际的情况要比上述的要求还复杂,在类别中还分小类,还要保证被拆分的小类的数量都能放入某一个仓库中。
    现在就是想知道要怎么算。曾经想过用双while循环,但不知道怎么判断跳出while,这是一个不知道要while多少次的循环。
      

  4.   

    Create table #
    (
    ID tinyint,
    [Name] varchar(50),
    [类别] varchar(50),
    [最大数量] tinyint,
    [已放数量] tinyint,
    [剩余数量] tinyint
    );
    insert into #(ID,[Name],[类别],[最大数量],[已放数量],[剩余数量])
    select * from (
    select 1 as ID,'仓库1' as [Name],'电器类' as [类别],35 as [最大数量],25 as [已放数量],10 as [剩余数量] union all
    select 2,'仓库2','电器类',30,28,2 union all
    select 3,'仓库3','电器类',25,21,4 union all
    select 4,'仓库4','电器类',35,25,10 union all
    select 5,'仓库5','电器类',35,19,16  
    )a;select * from # ;declare @已放数量 tinyint
    declare @数量 tinyintif (select sum(剩余数量) from # where id <> 5) >= (select 已放数量 from # where id = 5)
    begin
      select @已放数量 = 已放数量 from # where id = 5
      update # set
        @数量 = case when 剩余数量 <= @已放数量 then 剩余数量 else @已放数量 end
    ,@已放数量 = @已放数量 - @数量
    ,[已放数量] = [已放数量] + @数量
    ,[剩余数量] = [剩余数量] - @数量
      where id <> 5  update # set
         [已放数量] = @已放数量
    ,[剩余数量] = [最大数量] - @已放数量
      where id = 5

      print '拆分后结果:'
      select * from #
    end  
    else
      print '不可以拆分'
      
      
      drop table #
    --结果
    拆分后结果:1 仓库1 电器类 35 35 0
    2 仓库2 电器类 30 30 0
    3 仓库3 电器类 25 25 0
    4 仓库4 电器类 35 28 7
    5 仓库5 电器类 35 0 35
      

  5.   


    确实是。如果能用程序的话,应该能舒服不少。
    因为一个2003年的一个站点,用的是老技术asp+SQL2005;用户又需要实时效果,所以只能要Proc,这样执行效率可以保证。