--示例--测试数据 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 行) --*/
长进林场 红松 2222
百思林场 杨树 3333
大胜林场 红松、杨树 1:2 3000
乾安林场 落叶松、樟树 3:2 5000通过查询语句应得到的表内容如下林种 需苗量红松 4333
杨树 5333
落叶松 3000
樟树 2000会有这种情况红松、杨树、落叶松 1:2:3
长进林场 红松 2222
百思林场 杨树 3333
大胜林场 红松、杨树 1:2 3000
乾安林场 落叶松、樟树 3:2 5000通过查询语句应得到的表内容如下林种 需苗量红松 4333
杨树 5333
落叶松 3000
樟树 2000
小弟SQL比较水啊,提供个思路。
首选统计林种列里面的、号数,来确定有几种数,然后根据这个确定数量将林场、林种、数量写到新的表里面,最后在新表里面统计数据。
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 行)
--*/