--示例--测试数据
create table tb(单位 varchar(10),林种 varchar(100),混交比例 varchar(100),需苗量 int)
insert tb select '兰山林场','红松'       ,''    ,1111
union all select '长进林场','红松'       ,''    ,2222
union all select '百思林场','杨树'       ,''    ,3333
union all select '大胜林场','红松、杨树'  ,'1:2',3000
union all select '乾安林场','落叶松、樟树','3:2',5000
go--处理
declare @i int
select @i=max(case when len(林种)>len(混交比例) then len(林种) else len(混交比例) end)
from tb
set rowcount @i
select id=identity(int) into #t from syscolumns a,syscolumns b
set rowcount 0select id=0,a.单位
,林种=substring(a.林种,b.id,charindex('、',a.林种+'、',b.id)-b.id)
into #t1
from tb a,#t b
where substring('、'+a.林种,b.id,1)='、'
order by a.单位,b.idselect id=0,a.单位
,混交比例=substring(a.混交比例,b.id,charindex(':',a.混交比例+':',b.id)-b.id)
,a.需苗量
into #t2
from tb a,#t b
where substring(':'+a.混交比例,b.id,1)=':'
order by a.单位,b.iddeclare @单位 varchar(10)
update #t1 set @i=case when @单位=单位 then @i+1 else 1 end
,id=@i, @单位=单位set @单位=null
update #t2 set @i=case when @单位=单位 then @i+1 else 1 end
,id=@i, @单位=单位select a.林种,需苗量=sum(case when b.混交比例='' then b.需苗量 else b.混交比例*c.单位值 end)
from #t1 a,#t2 b,(
select 单位,单位值=max(需苗量)/sum(cast(混交比例 as int))
from #t2 group by 单位
)c where a.单位=b.单位 and a.id=b.id and a.单位=c.单位
group by a.林种select * from #t1
drop table #t,#t1,#t2
go--删除测试
drop table tb/*--测试结果林种               需苗量     
----------------- -----------
红松               4333
落叶松             3000
杨树               5333
樟树               2000(所影响的行数为 4 行)
--*/

解决方案 »

  1.   

    回复人: zjcxc(邹建) ( ) 信誉:439 
    --示例--测试数据
    create table tb(单位 varchar(10),林种 varchar(100),混交比例 varchar(100),需苗量 int)
    insert tb select '兰山林场','红松'       ,''    ,1111
    union all select '长进林场','红松'       ,''    ,2222
    union all select '百思林场','杨树'       ,''    ,3333
    union all select '大胜林场','红松、杨树'  ,'1:2',3000
    union all select '乾安林场','落叶松、樟树','3:2',5000
    go--处理
    declare @i int
    select @i=max(case when len(林种)>len(混交比例) then len(林种) else len(混交比例) end)
    from tb
    set rowcount @i
    select id=identity(int) into #t from syscolumns a,syscolumns b
    set rowcount 0select id=0,a.单位
    ,林种=substring(a.林种,b.id,charindex('、',a.林种+'、',b.id)-b.id)
    into #t1
    from tb a,#t b
    where substring('、'+a.林种,b.id,1)='、'
    order by a.单位,b.idselect id=0,a.单位
    ,混交比例=substring(a.混交比例,b.id,charindex(':',a.混交比例+':',b.id)-b.id)
    ,a.需苗量
    into #t2
    from tb a,#t b
    where substring(':'+a.混交比例,b.id,1)=':'
    order by a.单位,b.iddeclare @单位 varchar(10)
    update #t1 set @i=case when @单位=单位 then @i+1 else 1 end
    ,id=@i, @单位=单位set @单位=null
    update #t2 set @i=case when @单位=单位 then @i+1 else 1 end
    ,id=@i, @单位=单位select a.林种,需苗量=sum(case when b.混交比例='' then b.需苗量 else b.混交比例*c.单位值 end)
    from #t1 a,#t2 b,(
    select 单位,单位值=max(需苗量)/sum(cast(混交比例 as int))
    from #t2 group by 单位
    )c where a.单位=b.单位 and a.id=b.id and a.单位=c.单位
    group by a.林种select * from #t1
    drop table #t,#t1,#t2
    go--删除测试
    drop table tb/*--测试结果林种               需苗量     
    ----------------- -----------
    红松               4333
    落叶松             3000
    杨树               5333
    樟树               2000(所影响的行数为 4 行)
    --*/