A  1
A  2
A  3
A  4
B  5
B  6
B  7
C  8
C  9
D  10
D  11
要求查询成表:
A  B  C  D
1  5  8  10 
2  6  9  11
3  7
4

解决方案 »

  1.   

    create table tb(col char(1) , val int)
    insert into tb values('A',     1) 
    insert into tb values('A',     2) 
    insert into tb values('A',     3) 
    insert into tb values('A',     4) 
    insert into tb values('B',     5) 
    insert into tb values('B',     6) 
    insert into tb values('B',     7) 
    insert into tb values('C',     8) 
    insert into tb values('C',     9) 
    insert into tb values('D',     10) 
    insert into tb values('D',     11) 
    goselect px , 
      max(case col when 'A' then cast(val as varchar) else '' end) 'A',
      max(case col when 'B' then cast(val as varchar) else '' end) 'B',
      max(case col when 'C' then cast(val as varchar) else '' end) 'C',
      max(case col when 'D' then cast(val as varchar) else '' end) 'D'
    from
    (
      select * , px = (select count(1) from tb where col = t.col and val < t.val) + 1 from tb t
    ) t
    group by px drop table tb/*
    px          A                              B                              C                              D
    ----------- ------------------------------ ------------------------------ ------------------------------ ------------------------------
    1           1                              5                              8                              10
    2           2                              6                              9                              11
    3           3                              7                                                             
    4           4                                                                                            (4 行受影响)
    */
      

  2.   

    create table tb(col char(1) , val int)
    insert into tb values('A',     1) 
    insert into tb values('A',     2) 
    insert into tb values('A',     3) 
    insert into tb values('A',     4) 
    insert into tb values('B',     5) 
    insert into tb values('B',     6) 
    insert into tb values('B',     7) 
    insert into tb values('C',     8) 
    insert into tb values('C',     9) 
    insert into tb values('D',     10) 
    insert into tb values('D',     11) 
    goselect A,B,C,D from
    (
      select px , 
        max(case col when 'A' then cast(val as varchar) else '' end) 'A',
        max(case col when 'B' then cast(val as varchar) else '' end) 'B',
        max(case col when 'C' then cast(val as varchar) else '' end) 'C',
        max(case col when 'D' then cast(val as varchar) else '' end) 'D'
      from
      (
        select * , px = (select count(1) from tb where col = t.col and val < t.val) + 1 from tb t
      ) t
      group by px 
    ) mdrop table tb/*
    A                              B                              C                              D
    ------------------------------ ------------------------------ ------------------------------ ------------------------------
    1                              5                              8                              10
    2                              6                              9                              11
    3                              7                                                             
    4                                                                                            (4 行受影响)
    */
      

  3.   

    create table ko(val varchar(10),num int)
    insert into ko select 'A',1
    insert into ko select 'A',2
    insert into ko select 'A',3
    insert into ko select 'A',4
    insert into ko select 'B',5
    insert into ko select 'B',6
    insert into ko select 'B',7
    insert into ko select 'C',8
    insert into ko select 'C',9
    insert into ko select 'D',10
    insert into ko select 'D',11先将数据插入临时表
    select bh=(select count(1)+1 from ko where val=a.val and num<a.num),* into #temp from ko a然后执行下面:
    declare @sql varchar(1000)
    set @sql=''
    select @sql=@sql+',['+val+']=max(case val when '''+ val +''' then num else 0 end)' from (select distinct val from #temp)a
    set @sql='select '+right(@sql,len(@sql)-1)+' from #temp group by bh'
    exec(@sql)
      

  4.   

    本人新手问个问题,为什么要
    max(case col when 'A' then cast(val as varchar) else '' end) 'A',呢
    如果GROUP BY PX了当PX=1时,拿出了A 1,B 5,C 8,D 10。如果用CASE语句应该只有一个匹配的啊,用MAX和MIN应该一样吧
    但我把代码换成MIN就答案错了为什么用MAX啊,小弟新手啊