列名寫錯了,應是:
col1 col2 col3
1        2             q
1        8             k
2        7             e
2        9             h
要求返回
col1 col2 col3 col4 col5
1 2 q 8 k
2 7 e 9 h

解决方案 »

  1.   

    select * into txt
    from(
    select '1' as col1 ,           '2'  as col2,           'q' as col3
    union
    select '1'     ,   '8' ,            'k'
    union
    select '2'     ,   '7'   ,          'e'
    union
    select '2'     ,   '9'      ,       'h') ss*/
    go
    select a.*,b.col2,b.col3 from txt a,txt b where a.col1=b.col1 and a.col2<>b.col2 and a.col3<>b.col3
      

  2.   

    select col1,max(col2) col2,max(col3) col3,max(col4) col4,max(col5) col5
    from
    (select col1,col2,col3,'    ' as col4,'    ' as col5 
    from tablename 
    where col1+col2 in 
    (select col1+col2 from (select col1,min(col2) col2 from tablename group by col1) as  b)
    union
    select col1,'' col2,'' col3,col2 as col4,col3 as col5 
    from tablename 
    where col1+col2 in 
    (select col1+col2 from (select col1,max(col2) col2 from tablename group by col1) as c)
    ) as a
    group by col1