本帖最后由 fangzh110 于 2011-03-14 08:52:02 编辑

解决方案 »

  1.   

    declare int @st1,@st2,@st3,@st4
    select @st1= sum(*) from table where T1='a';
    select @st2= sum(*) from table where T1='b';
    ........
      

  2.   

    --> 测试数据: #tb
    if object_id('tempdb.dbo.#tb') is not null drop table #tb
    go
    create table #tb (id int,T1 varchar(111) )
    insert into #tb
    select 1,'a,b,c' union all
    select 2,'b,c,d,e' union all
    select 3,'a,c,d,f' union all
    select 4,'a,c' select distinct SUBSTRING(T1,number,CHARINDEX(',',t1+',',number)-number)
    from #tb,master..spt_values 
    where type='p' and number>0
    and SUBSTRING(','+T1,number,1)=','/*
    (4 row(s) affected)---------------------------------------------------------------------------------------------------------------
    a
    b
    c
    d
    e
    f(6 row(s) affected)
    */select  SUBSTRING(T1,number,CHARINDEX(',',t1+',',number)-number),COUNT(*)
    from #tb,master..spt_values 
    where type='p' and number>0
    and SUBSTRING(','+T1,number,1)=','
    group by SUBSTRING(T1,number,CHARINDEX(',',t1+',',number)-number)/*
    --------------------------------------------------------------------------------------------------------------- -----------
    a                                                                                                               3
    b                                                                                                               2
    c                                                                                                               4
    d                                                                                                               2
    e                                                                                                               1
    f                                                                                                               1(6 row(s) affected)
    */
      

  3.   

    create table #temp(id int identity, T1 varchar(10))
    insert #temp
    select 'a,b,c' union all
    select 'b,c,d,e' union all
    select 'a,c,d,f' union all
    select 'a,c'
    --SQL
    --#0.定义临时表#
    create table #(id int identity, T1 varchar(10))
    declare @sql varchar(max)
    set @sql = 'insert into # select T1=''' + replace(stuff((select ',' + T1 from #temp for xml path('')),1,1,''), ',', ''' union all select ''') + ''''
    exec(@sql)
    --#1.
    select T1=stuff((select distinct ','+T1 from # for xml path('')),1,1,'')
    /*
    T1
    a,b,c,d,e,f
    */
    --#2.
    select result=LTRIM((select ' ' + (T1 + '=' + cast(COUNT(*) as varchar(10))) from # group by T1 for xml path('')))
    /*
    result
    a=3 b=2 c=4 d=2 e=1 f=1
    */
      

  4.   

    --建表
    create table #tab(id int, t1 varchar(20))insert into #tab
    select 1,'a,b,c'
    union all select 2,'b,c,d,e'
    union all select 3,'a,c,d,f'
    union all select 4,'a,c'declare @sql varchar(1000)
    set @sql='select * into ##temp from (select '''
    select @Sql=@sql+t1+',' from #tab
    select @sql=replace(@sql,',',''' as a union all select'''),@sql=(substring(@sql,1,len(@sql)-22)+') as a')
    exec (@sql)--1字符合并
    declare @Merger varchar(20)
    set @Merger=''
    select  @Merger=@Merger+','+ a from ##temp group by aselect substring(@Merger,2,len(@Merger)) --结果1-------------------
    a,b,c,d,e,f(1 行受影响)select a,count(0) from ##temp group by a
    --结果2
    a    
    ---- -----------
    a    3
    b    2
    c    4
    d    2
    e    1
    f    1(6 行受影响)