select identity(int,1,1)xh,* into #t from 表
select aa.c as a1,bb.c as b1,cc.c as c1
from #t aa left join #t b on aa.xh=bb.xh-1 
left join #t cc on aa.xh=cc.xh-2
where (aa.xh-1)%3=0

解决方案 »

  1.   

    select identity(int,1,1) as idd,C into #t from yourtable
    select A.c as a1,B.c as b1,C.c as c1 from #t A left join #t b on A.idd=b.idd-1
     left join #t C on A.idd=C.idd-2 where (a.idd-1)%3=0
      

  2.   

    musicdog(白狮子),你可以试试速度的啊.:)
      

  3.   

    Select a1=Max(Case when b='name' then c else '' end)
           b1=Max(Case when b='sex' then c  else '' end)
           c1=Max(Case when b='age' then c  else ''end)
       from 表 Group by a
      

  4.   

    CrazyFor(太阳下山明朝依旧爬上来) ,谢谢你,我手里只有很少模拟数据。我还想知道其他的解决办法。然后我一起拿到客户那边试。
      

  5.   

    Select a1=Max(Case when b='name' then c else '' end),
           b1=Max(Case when b='sex' then c  else '' end),
           c1=Max(Case when b='age' then c  else ''end)
       from 表 Group by a
      

  6.   

    SELECT A.ID,MAX(A.A1) as A1,MAX(A.B1) AS B1,MAX(A.C1) AS C1 INTO 目标表 FROM
      (SELECT a as ID, CASE when b='name' then c end AS a1, 
           CASE when b='sex' then c end as b1,
           CASE when b='age' then c end as c1
         FROM 原始表) A          --内部的临时表非常有用!!
    GROUP BY A.ID 
      

  7.   

    SELECT A.ID,MAX(A.A1) as A1,MAX(A.B1) AS B1,MAX(A.C1) AS C1 INTO 目标表 FROM
      (SELECT a as ID, CASE when b='name' then c else '' end AS a1, 
           CASE when b='sex' then c else '' end as b1,
           CASE when b='age' then c else '' end as c1
         FROM 原始表) A          --内部的临时表非常有用!!
    GROUP BY A.ID 
    不经过内部表会出乱子的!!
      

  8.   

    97866(weiLuang) 你的对了。因为我总想着sum对数字有效,就思维定式了,我忽略了MAX对字符串有效。
      

  9.   

    select 
    max(case when b='name' then c end) a1,
    max(case when b='sex' then c end) a2,
    max(case when b='age' then c end) a3
    from 表 group by a