现在有这样一种情况
数据格式如下:开始月  结束月  总量  销售员
  1       3      900   张三
  2       5     2400   李四想这样的数据的格式,想要得到的结果如下:月份 销售量 销售员
  1    300   张三
  2    300   张三
  3    300   张三
  2    600   李四
  3    600   李四
  4    600   李四
  5    600   李四意思也就是说向把一条销售的记录拆分成多条,中间涉及到月份的按照
开始月份与结束月份取中间的月份,量是总量除以月份的总数取的平均值,
不知道这样说是不是清楚了
  
谢谢   

解决方案 »

  1.   

    ---测试数据---
    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([开始月] int,[结束月] int,[总量] int,[销售员] varchar(4))
    insert [tb]
    select 1,3,900,'张三' union all
    select 2,5,2400,'李四'
     
    ---查询---
    select 
      开始月+b.number as 月份,
      总量/(结束月-开始月+1) as 销售量, 
      销售员
    from
      tb a
    join
      master..spt_values b
    on
      开始月+b.number<=结束月 and b.type='P'---结果---
    月份          销售量         销售员
    ----------- ----------- ----
    1           300         张三
    2           300         张三
    3           300         张三
    2           600         李四
    3           600         李四
    4           600         李四
    5           600         李四(7 行受影响)
      

  2.   

    thx  josy, let me have a try
      

  3.   

       直接sql不知道 用游标很方便use tempdb
    if object_id('dbo.A') is not null
    drop table dbo.A
    if object_id('dbo.B') is not null
    drop table dbo.Bcreate table dbo.A
    (
      startmonth int ,
      endmonth  int,
      total int ,
      seller varchar(100)
    )create table dbo.B
    (
      startmonth int ,
      total int ,
      seller varchar(100)
    )insert into dbo.A values(1,3,900,'张三')
    insert into dbo.A values(2,5,2400,'李四')declare @num int 
    declare @st int 
    declare @en int 
    declare @to int 
    declare @se varchar(100)
    declare cur cursor for select * from dbo.A
    open cur 
    fetch next from cur into @st , @en ,@to , @se
    while(@@fetch_status=0)
    begin
    set @num = @en-@st+1
    while(@st<=@en)
    begin
    insert into dbo.B values (@st,@to/@num,@se)
    set @st=@st+1
    end
    fetch next from cur into @st , @en ,@to,@se
    end
    close cur 
    deallocate cur
    select * from dbo.B
      

  4.   

    thx benluobobo123
    你的方法也是可以的,谢谢你们的帮助,结贴给分