--处理
select id=0,* into #t from 表 order by 编号,分类一--,分类二,分类三
declare @a varchar(10),@i int
update #t set @i=case @a when 分类一 then @i+1 else 0 end,id=@i,@a=分类一declare @fd varchar(8000),@s varchar(8000)
,@t1 varchar(10),@t2 varchar(10)
select @fd='',@s='',@t1='',@t2=''
select @t2='['+分类一+']'
,@fd=@fd+','+@t2+'=isnull('+@t2+'.分类二,''''),'+@t2+'=isnull('+@t2+'.分类二,'''')'
,@s=case @t1 when '' then '(select * from #t where 分类一='''+分类一+''')'+@t2 
else @s+' full join(select * from #t where 分类一='''+分类一+''')'+@t2+' on '+@t1+'.id='+@t2+'.id ' end
,@t1=@t2
from #t group by 分类一select @fd=substring(@fd,2,8000)
exec('select '+@fd+'
from '+@s)--删除处理临时表
drop table #t

解决方案 »

  1.   

    --测试--测试数据
    create table 表(编号 int,分类一 varchar(10),分类二 varchar(10),分类三 varchar(10))
    insert 表 select 1,'M1','D1','F1'
    union all select 1,'M1','D2','F2'union all select 1,'M2','D3','F3'
    union all select 1,'M2','D3','F4'
    union all select 1,'M2','D3','F5'
    union all select 1,'M2','D4','F2'
    union all select 1,'M2','D4','F7'
    union all select 1,'M2','D5','F1'
    union all select 1,'M2','D5','F3'union all select 1,'M3','D6','F8'
    union all select 1,'M3','D7','F6'
    go--处理
    select id=0,* into #t from 表 order by 编号,分类一--,分类二,分类三
    declare @a varchar(10),@i int
    update #t set @i=case @a when 分类一 then @i+1 else 0 end,id=@i,@a=分类一declare @fd varchar(8000),@s varchar(8000)
    ,@t1 varchar(10),@t2 varchar(10)
    select @fd='',@s='',@t1='',@t2=''
    select @t2='['+分类一+']'
    ,@fd=@fd+','+@t2+'=isnull('+@t2+'.分类二,''''),'+@t2+'=isnull('+@t2+'.分类二,'''')'
    ,@s=case @t1 when '' then '(select * from #t where 分类一='''+分类一+''')'+@t2 
    else @s+' full join(select * from #t where 分类一='''+分类一+''')'+@t2+' on '+@t1+'.id='+@t2+'.id ' end
    ,@t1=@t2
    from #t group by 分类一select @fd=substring(@fd,2,8000)
    exec('select '+@fd+'
    from '+@s)--删除处理临时表
    drop table #t
    go --删除测试
    drop table 表/*--测试结果M1         M1         M2         M2         M3         M3         
    ---------- ---------- ---------- ---------- ---------- ---------- 
    D1         D1         D3         D3         D6         D6
    D2         D2         D3         D3         D7         D7
                          D3         D3                    
                          D4         D4                    
                          D4         D4                    
                          D5         D5                    
                          D5         D5                    --*/
      

  2.   

    zjcxc(邹建) :
    你看看你的结果是不是和我的一样呢?
    :)
      

  3.   

    --打错字段名而已,不是方法有问题--处理
    select id=0,* into #t from 表 order by 编号,分类一--,分类二,分类三
    declare @a varchar(10),@i int
    update #t set @i=case @a when 分类一 then @i+1 else 0 end,id=@i,@a=分类一declare @fd varchar(8000),@s varchar(8000)
    ,@t1 varchar(10),@t2 varchar(10)
    select @fd='',@s='',@t1='',@t2=''
    select @t2='['+分类一+']'
    ,@fd=@fd+','+@t2+'=isnull('+@t2+'.分类二,''''),'+@t2+'=isnull('+@t2+'.分类三,'''')'
    ,@s=case @t1 when '' then '(select * from #t where 分类一='''+分类一+''')'+@t2 
    else @s+' full join(select * from #t where 分类一='''+分类一+''')'+@t2+' on '+@t1+'.id='+@t2+'.id ' end
    ,@t1=@t2
    from #t group by 分类一select @fd=substring(@fd,2,8000)
    exec('select '+@fd+'
    from '+@s)--删除处理临时表
    drop table #t
      

  4.   

    --测试--测试数据
    create table 表(编号 int,分类一 varchar(10),分类二 varchar(10),分类三 varchar(10))
    insert 表 select 1,'M1','D1','F1'
    union all select 1,'M1','D2','F2'union all select 1,'M2','D3','F3'
    union all select 1,'M2','D3','F4'
    union all select 1,'M2','D3','F5'
    union all select 1,'M2','D4','F2'
    union all select 1,'M2','D4','F7'
    union all select 1,'M2','D5','F1'
    union all select 1,'M2','D5','F3'union all select 1,'M3','D6','F8'
    union all select 1,'M3','D7','F6'
    go--处理
    select id=0,* into #t from 表 order by 编号,分类一--,分类二,分类三
    declare @a varchar(10),@i int
    update #t set @i=case @a when 分类一 then @i+1 else 0 end,id=@i,@a=分类一declare @fd varchar(8000),@s varchar(8000)
    ,@t1 varchar(10),@t2 varchar(10)
    select @fd='',@s='',@t1='',@t2=''
    select @t2='['+分类一+']'
    ,@fd=@fd+','+@t2+'=isnull('+@t2+'.分类二,''''),'+@t2+'=isnull('+@t2+'.分类三,'''')'
    ,@s=case @t1 when '' then '(select * from #t where 分类一='''+分类一+''')'+@t2 
    else @s+' full join(select * from #t where 分类一='''+分类一+''')'+@t2+' on '+@t1+'.id='+@t2+'.id ' end
    ,@t1=@t2
    from #t group by 分类一select @fd=substring(@fd,2,8000)
    exec('select '+@fd+'
    from '+@s)--删除处理临时表
    drop table #t
    go --删除测试
    drop table 表/*--测试结果
    M1         M1         M2         M2         M3         M3         
    ---------- ---------- ---------- ---------- ---------- ---------- 
    D1         F1         D3         F3         D6         F8
    D2         F2         D3         F4         D7         F6
                          D3         F5                    
                          D4         F2                    
                          D4         F7                    
                          D5         F1                    
                          D5         F3                    --*/
      

  5.   

    --另一种处理方法--测试--测试数据
    create table 表(编号 int,分类一 varchar(10),分类二 varchar(10),分类三 varchar(10))
    insert 表 select 1,'M1','D1','F1'
    union all select 1,'M1','D2','F2'union all select 1,'M2','D3','F3'
    union all select 1,'M2','D3','F4'
    union all select 1,'M2','D3','F5'
    union all select 1,'M2','D4','F2'
    union all select 1,'M2','D4','F7'
    union all select 1,'M2','D5','F1'
    union all select 1,'M2','D5','F3'union all select 1,'M3','D6','F8'
    union all select 1,'M3','D7','F6'
    go--处理方法2.
    declare @s varchar(8000)
    select @s=''
    select @s=@s+',['+分类一+'_1] varchar(10) default '''',['
    +分类一+'_2] varchar(10) default '''''
    from 表 group by 分类一exec('create table #t(id int identity(1,1)'+@s+')
    declare @s varchar(8000)
    declare tb cursor local for select s=''declare @t table(id int identity(1,1),a varchar(10),b varchar(10))
    declare @r int
    insert @t select 分类二,分类三 from 表 where 分类一=''''''+分类一+''''''
    set @r=@@rowcount
    update #t set [''+分类一+''_1]=b.a,[''+分类一+''_2]=b.b
    from #t a join @t b on a.id=b.id
    if @@rowcount<@r 
    insert #t([''+分类一+''_1],[''+分类一+''_2]) select a.a,a.b 
    from @t a left join #t b on a.id=b.id
    where b.id is null''
    from 表 group by 分类一
    open tb 
    fetch next from tb into @s
    while @@fetch_status=0
    begin
    exec(@s)
    fetch next from tb into @s
    end
    close tb
    deallocate tb
    select * from #t
    ')
    go --删除测试
    drop table 表/*--测试结果
    M1         M1         M2         M2         M3         M3         
    ---------- ---------- ---------- ---------- ---------- ---------- 
    D1         F1         D3         F3         D6         F8
    D2         F2         D3         F4         D7         F6
                          D3         F5                    
                          D4         F2                    
                          D4         F7                    
                          D5         F1                    
                          D5         F3                    --*/