日期:2014-05-19  浏览次数:20424 次

老问题,还是求一统计SQL语句,解决马上给分...
求SQL语句,解决马上给分:

表:v_PtStore

F_ID           F_Specs                         F_StoreNumber
1                 1.0*2.0*6000               1
2                 1.0*2.0*6000               2
3                 1.15*2.25*6000           5
4                 1.2*3.2*6000               1
5                 20*30*1.2*6000           1
5                 20*50*1.2*6000           1

要求统计结果:

              1.0         1.15         1.2                
2.0         3            
2.25                     5      
3.2                                       1
20*30                                   1
20*50                                   1

大概有几千种F_Specs。谢谢

------解决方案--------------------
create table xyz (F_ID int, F_Specs varchar(100), F_StoreNumber int)
insert xyz select 1 , '1.0*2.0*6000 ', 1
union all select 2 , '1.0*2.0*6000 ', 2
union all select 3 , '1.15*2.25*6000 ', 5
union all select 4 , '1.2*3.2*6000 ', 1


select f_id,a=left(f_specs,charindex( '* ',f_specs)-1),
b=substring(f_specs,charindex( '* ',f_specs)+1, charindex( '* ',f_specs,charindex( '* ',f_specs)+1)-charindex( '* ',f_specs)-1),
c=right(f_specs,len(f_specs)-charindex( '* ',f_specs,charindex( '* ',f_specs)+1)),
f_storenumber into # from xyz

declare @a varchar(1000)
select @a=isnull(@a+ ', ', ' ')+ ' sum(case when b= '+ltrim(b)+ ' then f_storenumber else 0 end) [ ' +ltrim(b)+ '] ' from # group by b
exec( 'select a, '+@a + ' from # group by a ')

drop table #
------解决方案--------------------
create table v_PtStore (F_ID int,F_Specs varchar(20),F_StoreNumber int)

insert into v_PtStore values(1, '1.0*2.0*6000 ' , 1)
insert into v_PtStore values(2 , '1.0*2.0*6000 ' , 2)
insert into v_PtStore values(3 , '1.15*2.25*6000 ', 5)
insert into v_PtStore values(4 , '1.2*3.2*6000 ' , 1)
select cast(substring(f_specs , 1 , charindex( '* ' , f_specs) - 1) as varchar) f_specs1, cast(substring(f_specs , charindex( '* ' , f_specs) + 1 , charindex( '* ',f_specs,charindex( '* ' , f_specs) + 1) - charindex( '* ' , f_specs) - 1) as varchar) f_specs2, F_StoreNumber into #temp from v_ptstore
go

declare @sql varchar(8000)
set @sql = 'select f_specs1 '