--测试--测试数据
create table 表(大类 varchar(10),小类 varchar(10))
insert 表 select '问题1','11'
union all select '问题1','12'
union all select '问题1','13'
union all select '问题2','21'
union all select '问题2','22'
union all select '问题3','31'
union all select '问题3','32'
union all select '问题3','33'
go--处理
select id=0,大类,小类 into #tb from 表 order by 大类,小类
declare @i int,@类别 varchar(10)
update #tb set @i=case @类别 when 大类 then @i+1 else 1 end,id=@i,@类别=大类select top 8000 id=identity(int,1,1) into #t from sysobjects,syscolumnsselect 大类=isnull(a.大类,''),小类=isnull(b.小类,'')
from(
select id,大类1=大类,大类=substring(大类,id,1)
from(select 大类 from 表 group by 大类)a,#t b
where id<=len(大类)
)a full join #tb b on a.大类1=b.大类 and a.id=b.id
go--删除处理的临时表
drop table #tb,#t
go--删除测试数据
drop table 表/*--测试结果大类   小类         
---- ---------- 
问    11
题    12
1    13
问    21
题    22
2    
问    31
题    32
3    33(所影响的行数为 9 行)
--*/

解决方案 »

  1.   

    create table #t (大类 varchar(10), 小类   varchar(10))
    insert #t
    select
    '问题1',  '11'
    union all
    select
    '问题1',  '12'
    union all
    select
    '问题1',  '13'
    union all
    select
    '问题2',  '21'
    union all
    select
    '问题2',  '22'
    union all
    select
    '问题3',  '31'
    union all
    select
    '问题3',  '32'
    union all
    select
    '问题3',  '33'create table #r (大类 varchar(10), 小类   varchar(10),原大类 varchar(10))declare @i int
    set @i=1
    while exists (select * from #t)
    begin
    insert #r
    select left(right(大类,@i),1),小类,大类
            from #t a where 小类=(select max(小类) from #t where 大类=a.大类)
            delete a from #t a where 小类=(select max(小类) from #t where 大类=a.大类)
    set @i=@i+1
    endwhile exists (
    select distinct left(right(原大类,1+(select count(*) from #r where [原大类]=a.原大类)),1),'',原大类 from #r a where len(rtrim([原大类]))>(select count(*) from #r where [原大类]=a.原大类)
    )
    insert #r
    select distinct left(right(原大类,1+(select count(*) from #r where [原大类]=a.原大类)),1),'',原大类 from #r a where len(rtrim([原大类]))>(select count(*) from #r where [原大类]=a.原大类)select 大类,小类 from #r order by 原大类,小类
    delete #rdelete #t结果:
    大类         小类         
    ---------- ---------- 
    问          11
    题          12
    1          13
    问          
    题          21
    2          22
    问          31
    题          32
    3          33(所影响的行数为 9 行)
      

  2.   

    同意楼上.不得不承认SQL SERVER的人就是牛.
      

  3.   

    只要你想得出.
    他们就能用SQL表示.
      

  4.   

    用斑竹的方法:--测试数据
    create table 表(大类 varchar(10),小类 varchar(10))
    insert 表 select '问题1','11'
    union all select '问题1','12'
    union all select '问题1','13'
    union all select '问题2','21'
    union all select '问题2','22'
    union all select '问题3','31'
    union all select '问题3','32'
    union all select '问题3','33'
    go--处理
    select id=0,大类,小类 into #tb from 表 order by 大类,小类
    declare @i int,@类别 varchar(10)
    update #tb set @i=case @类别 when 大类 then @i+1 else 1 end,id=@i,@类别=大类update a
    set id=len(大类)-(select count(*) from #tb where 大类=a.大类)+id
    from #tb a
    select top 8000 id=identity(int,1,1) into #t from sysobjects,syscolumnsselect 大类=isnull(a.大类,''),小类=isnull(b.小类,'')
    from(
    select id,大类1=大类,大类=substring(大类,id,1)
    from(select 大类 from 表 group by 大类)a,#t b
    where id<=len(大类)
    )a full join #tb b on a.大类1=b.大类 and a.id=b.id
    go--删除处理的临时表
    drop table #tb,#t
    go--删除测试数据
    drop table 表
      

  5.   

    create table #t (大类 varchar(10), 小类   varchar(10))
    insert #t
    select
    '问题1',  '11'
    union all
    select
    '问题1',  '12'
    union all
    select
    '问题1',  '13'
    union all
    select
    '问题1',  '14'
    union all
    select
    '问题1',  '15'
    union all
    select
    '问题2',  '21'
    union all
    select
    '问题2',  '22'
    union all
    select
    '问题3',  '31'
    union all
    select
    '问题3',  '32'
    union all
    select
    '问题3',  '33'create table #r (大类 varchar(10), 小类   varchar(10),原大类 varchar(10))declare @i int
    set @i=1
    while exists (select * from #t)
    begin
    insert #r
    select left(right(大类,@i),1),小类,大类
            from #t a where 小类=(select max(小类) from #t where 大类=a.大类)
            delete a from #t a where 小类=(select max(小类) from #t where 大类=a.大类)
    set @i=@i+1
    endwhile exists (
    select distinct left(right(原大类,1+(select count(*) from #r where [原大类]=a.原大类)),1),'',原大类 from #r a where len(rtrim([原大类]))>(select count(*) from #r where [原大类]=a.原大类)
    )
    insert #r
    select distinct left(right(原大类,1+(select count(*) from #r where [原大类]=a.原大类)),1),'',原大类 from #r a where len(rtrim([原大类]))>(select count(*) from #r where [原大类]=a.原大类)select 大类,小类 from #r order by 原大类,小类
    drop table #rdrop table #t--结果
    问 11
    问 12
    问 13
    题 14
    1 15

    题 21
    2 22
    问 31
    题 32
    3 33
      

  6.   

    增加小类为5项,邹建的测试结果--测试--测试数据
    create table 表(大类 varchar(10),小类 varchar(10))
    insert 表 select '问题1','11'
    union all select '问题1','12'
    union all select '问题1','13'
    union all select '问题1','14'
    union all select '问题1','15'
    union all select '问题2','21'
    union all select '问题2','22'
    union all select '问题3','31'
    union all select '问题3','32'
    union all select '问题3','33'
    go--处理
    select id=0,大类,小类 into #tb from 表 order by 大类,小类
    declare @i int,@类别 varchar(10)
    update #tb set @i=case @类别 when 大类 then @i+1 else 1 end,id=@i,@类别=大类select top 8000 id=identity(int,1,1) into #t from sysobjects,syscolumnsselect 大类=isnull(a.大类,''),小类=isnull(b.小类,'')
    from(
    select id,大类1=大类,大类=substring(大类,id,1)
    from(select 大类 from 表 group by 大类)a,#t b
    where id<=len(大类)
    )a full join #tb b on a.大类1=b.大类 and a.id=b.id
    go--删除处理的临时表
    drop table #tb,#t
    go--删除测试数据
    drop table 表/*--测试结果
    问 11
    题 12
    1 13
    问 21
    题 22
    2
    问 31
    题 32
    3 33
    15
    14
    */
      

  7.   

    斑竹的方法,改排序--测试数据
    create table 表(大类 varchar(10),小类 varchar(10))
    insert 表 select '问题1','11'
    union all select '问题1','12'
    union all select '问题1','13'
    union all select '问题1','14'
    union all select '问题1','15'
    union all select '问题2','21'
    union all select '问题2','22'
    union all select '问题3','31'
    union all select '问题3','32'
    union all select '问题3','33'
    go--处理
    select id=0,大类,小类 into #tb from 表 order by 大类,小类
    declare @i int,@类别 varchar(10)
    update #tb set @i=case @类别 when 大类 then @i+1 else 1 end,id=@i,@类别=大类update a
    set id=len(大类)-(select count(*) from #tb where 大类=a.大类)+id
    from #tb a
    select top 8000 id=identity(int,1,1) into #t from sysobjects,syscolumnsselect 大类=isnull(a.大类,''),小类=isnull(b.小类,'')
    from(
    select id,大类1=大类,大类=substring(大类,id,1)
    from(select 大类 from 表 group by 大类)a,#t b
    where id<=len(大类)
    )a full join #tb b on a.大类1=b.大类 and a.id=b.id
    order by  a.大类1,b.小类go--删除处理的临时表
    drop table #tb,#t
    go--删除测试数据
    drop table 表
      

  8.   

    我的改成:create table #t (大类 varchar(10), 小类   varchar(10))
    insert #t
    select
    '问题1',  '11'
    union all
    select
    '问题1',  '12'
    union all
    select
    '问题1',  '13'
    union all
    select
    '问题1',  '14'
    union all
    select
    '问题1',  '15'
    union all
    select
    '问题2',  '21'
    union all
    select
    '问题2',  '22'
    union all
    select
    '问题3',  '31'
    union all
    select
    '问题3',  '32'
    union all
    select
    '问题3',  '33'create table #r (大类 varchar(10), 小类   varchar(10),原大类 varchar(10))declare @i int
    set @i=1
    while exists (select * from #t)
    begin
    insert #r
    select case when @i<=len(大类) then left(right(大类,@i),1) else '' end,小类,大类
            from #t a where 小类=(select max(小类) from #t where 大类=a.大类)         delete a from #t a where 小类=(select max(小类) from #t where 大类=a.大类)
    set @i=@i+1
    endwhile exists (
    select distinct left(right(原大类,1+(select count(*) from #r where [原大类]=a.原大类)),1),'',原大类 from #r a where len(rtrim([原大类]))>(select count(*) from #r where [原大类]=a.原大类)
    )
    insert #r
    select distinct left(right(原大类,1+(select count(*) from #r where [原大类]=a.原大类)),1),'',原大类 from #r a where len(rtrim([原大类]))>(select count(*) from #r where [原大类]=a.原大类)select 大类,小类 from #r order by 原大类,小类drop table #rdrop table #t
      

  9.   

    --加排序就行了:
    order by isnull(a.大类1,b.大类),isnull(a.id,b.id)
      

  10.   

    --测试--测试数据
    create table 表(大类 varchar(10),小类 varchar(10))
    insert 表 select '问题1','11'
    union all select '问题1','12'
    union all select '问题1','13'
    union all select '问题1','14'
    union all select '问题1','15'
    union all select '问题2','21'
    union all select '问题2','22'
    union all select '问题3','31'
    union all select '问题3','32'
    union all select '问题3','33'
    go--处理
    select id=0,大类,小类 into #tb from 表 order by 大类,小类
    declare @i int,@类别 varchar(10)
    update #tb set @i=case @类别 when 大类 then @i+1 else 1 end,id=@i,@类别=大类select top 8000 id=identity(int,1,1) into #t from sysobjects,syscolumnsselect 大类=isnull(a.大类,''),小类=isnull(b.小类,'')
    from(
    select id,大类1=大类,大类=substring(大类,id,1)
    from(select 大类 from 表 group by 大类)a,#t b
    where id<=len(大类)
    )a full join #tb b on a.大类1=b.大类 and a.id=b.id
    order by isnull(a.大类1,b.大类),isnull(a.id,b.id)
    go--删除处理的临时表
    drop table #tb,#t
    go--删除测试数据
    drop table 表/*--测试结果大类   小类         
    ---- ---------- 
    问    11
    题    12
    1    13
         14
         15
    问    21
    题    22
    2    
    问    31
    题    32
    3    33(所影响的行数为 11 行)
    --*/