create table Table1(项目 varchar(20),金额 int)
insert Table1 select 'X1' ,100
union all select 'X2' ,120
union all select 'Y1 Y3' ,180
union all select 'Y2' ,90
union all select 'Z1' ,140
union all select 'A1 A2 B1',300
union all select 'B2 Z2 C1' ,150
go
--查询处理
--处理临时表
declare @i int
select @i=max(len(项目)) from Table1
set rowcount @i
select id=identity(int) into #t from syscolumns a,syscolumns b
set rowcount 0
--统计出结果
select 项目,项目1=substring(a.项目,b.id,1),金额
into #t1 from Table1 a,#t b
where len(a.项目)>=b.id and patindex(' [^ ]%',substring(' '+a.项目,b.id,8000))=1
select 项目=a.项目1,金额=sum(a.金额/b.cnt)
from #t1 a,(select 项目,cnt=count(*) from #t1 group by 项目)b
where a.项目=b.项目
group by a.项目1
drop table #t,#t1
go
--删除测试
drop table Table1
/*--测试结果
项目 金额
---- -----------
A 200
B 150
C 50
X 220
Y 270
Z 190
select a.col1,sum( (b.金额/(len(b.项目)-len(replace(b.项目,' ',''))+1))
*(len(b.项目)-len(replace(b.项目,a.col1,'')))
) as 金额
from (select 'A' as col1 union all select
'B' as col1 union all select
'C' as col1 union all select
......
'X' as col1 ) a,表 b
where charindex(a.col1,b.项目)>0
group by a.col1
select a.col1,sum(b.金额/(len(b.项目)-len(replace(b.项目,' ',''))+1)) as 金额
from (select 'A' as col1 union all
'B' as col1 union all
...
'Z' as col1 ) a,表 b
where a.col1=charindex(a.col1,b.项目)>0
group by a.col1
select a.col1,sum(b.金额/(len(b.项目)-len(replace(b.项目,' ',''))+1)) as 金额
from (select 'A' as col1 union all
'B' as col1 union all
...
'Z' as col1 ) a,表 b
where a.col1=charindex(a.col1,b.项目)>0
group by a.col1 c
create table Table1(项目 varchar(10),金额 int)
insert Table1 select 'X1' ,100
union all select 'X2' ,120
union all select 'Y1 Y3' ,180
union all select 'Y2' ,90
union all select 'Z1' ,140
union all select 'A1 A2 B1',300
union all select 'B2 Z2 C1',150
go
--查询处理
--处理临时表
declare @i int
select @i=max(len(项目)) from Table1
set rowcount @i
select id=identity(int) into #t from syscolumns a,syscolumns b
set rowcount 0
--统计出结果
select 项目=substring(a.项目,b.id,1)
,金额=sum(a.金额/(len(a.项目)-len(replace(a.项目,' ',''))+1))
from Table1 a,#t b
where len(a.项目)>=b.id and charindex(' ',' '+a.项目,b.id)=b.id
group by substring(a.项目,b.id,1)
drop table #t
go
--删除测试
drop table Table1
/*--测试结果
项目 金额
---- -----------
A 200
B 150
C 50
X 220
Y 270
Z 190