小修正,倒数第4行开始:
set @exec='select ['+@key_col+'],'+@exec+' from ['+@table_to_turn+'] group by ['+@key_col+']'exec(@exec)
go

解决方案 »

  1.   

    join 那里也有一个错:
    select t0.name,t1.value as 'a',t2.value as 'c',t3.value as 'b', t4.value as 'd'
    from 
    (select distinct name from unknown2) t0,
    (select name,item from unknown2 where item='a') t1,
    (select name,item from unknown2 where item='c') t2,
    (select name,item from unknown2 where item='b') t3,
    (select name,item from unknown2 where item='d') t4
    where 
    t0.name*=t1.name and t0.name*=t2.name and t0.name*=t3.name and t0.name*=t4.name
      

  2.   

    2、纵横转换
    --脑袋 不好使错误太多干脆重贴一遍a、横到纵
    有表 unknown
    name a c b d
    Tom 1 2 3 4
    Sun 1 2 3 4
    要求以纵向格式显示
    这个最简单一直union 下来就是 
    select name,'a' as item,a as value from unknown
    union all
    select name,'b' as item,b as value from unknown
    union all
    select name,'c' as item,c as value from unknown
    union all
    select name,'d' as item,d as value from unknown结果有人说了,你这个格式不符合要求,怎么一会儿Tom,一会儿 Sum 的看得我都晕了,我要按照
    tom a 1
    tom c 2
    tom b 3
    tom d 4
    Sun a 1
    这样的格式排下来,你心里可能会开始骂:真多事,给你列出来不就得了,还这么多要求
    尝试:
    select * from 
    (
    select name,'a' as item,a as value from unknown
    union all
    select name,'b' as item,b as value from unknown
    union all
    select name,'c' as item,c as value from unknown
    union all
    select name,'d' as item,d as value from unknown
    ) view1 
    order by name,item或select name,'a' as item,a as value from unknown
    union 
    select name,'b' as item,b as value from unknown
    union 
    select name,'c' as item,c as value from unknown
    union 
    select name,'d' as item,d as value from unknown结果又有人挑骨头:在我的表里c是在b前面的,你怎么跑后面去了!
    想到最后,聪明的你被逼无奈最后一定会想出了很下流的一招:系统表。
    select view1.* from 
    (
    select name,'a' as item,a as value from unknown
    union all
    select name,'b' as item,b as value from unknown
    union all
    select name,'c' as item,c as value from unknown
    union all
    select name,'d' as item,d as value from unknown
    ) view1 ,
    syscolumns s
    where view1.item *= s.name where id=object_id('unknown')
    order by view1.name,s.colid
    --注意这个*= 不是笔误,是 left outer join 的意思
    a、纵到横
    这世道总有人想不开,你刚把数据转成了 unknown2
    name item value
    tom a 1
    tom c 2
    tom b 3
    tom d 4
    Sun a 1
    Sun c 2
    Sun b 3
    Sun d 4
    又有人要你把它转回来。
    没办法,开始分析:
    转回来的意思就是行只剩下 Tom和 Sun 两个单位了,列却要多了起来,等等,列要多起来?什么时候会多起来?自然是join 了!好:select t0.name,t1.value as 'a',t2.value as 'c',t3.value as 'b', t4.value as 'd'
    from 
    (select distinct name from unknown2) t0,
    (select name,item from unknown2 where item='a') t1,
    (select name,item from unknown2 where item='c') t2,
    (select name,item from unknown2 where item='b') t3,
    (select name,item from unknown2 where item='d') t4
    where 
    t0.name*=t1.name and t0.name*=t2.name and t0.name*=t3.name and t0.name*=t4.name但如果用另外一个方向想,怎么才能把行压缩成两行呢?只有group by,但怎么区分在不同列里显示的不同内容呢?最好的答案是case:

    select name,
    sum(case when item='a' then value else 0 end) as a,
    sum(case when item='c' then value else 0 end) as c,
    sum(case when item='b' then value else 0 end) as b,
    sum(case when item='d' then value else 0 end) as d
    from unknown2
    group by name至于其他再复杂的纵横转换,也就是上面几种思路的交叉而已了。
    至于如果设计 列数待定的情况则只能用过程来解决了。
    附:一个通用从纵到横的简单转换存储过程
    if object_id('make_fun') is not null
    drop procedure make_fun
    go
    create procedure make_fun
    (@table_to_turn varchar(255), --待旋转的表
    @key_col varchar(255), --保留的关键字段
    @col_know_how varchar(255), --生成列名的字段
    @col_to_turn varchar(255), --作为值的字段
    @how_to varchar(20)='sum') --生成值的方式 sum min max avg ,etc.
    /*
    过程作用,根据纵向数据生成新横向结构
    by realgz@csdn 2003-12-26
    */
    as
    declare @exec varchar(8000)create table #tmp (col varchar(255))set @exec='select distinct '+@col_know_how+ ' from '+@table_to_turn
    insert into #tmp exec (@exec)
    set @exec=''select @exec=@exec+@how_to+'(case when ['+@col_know_how+']= '''+col+''' then ['+@col_to_turn +'] else null end ) as ['+col+'],'
    from #tmp 
    set @exec=left(@exec,len(@exec)-1)
    set @exec='select ['+@key_col+'],'+@exec+' from ['+@table_to_turn+'] group by ['+@key_col+']'exec(@exec)
    go
      

  3.   

    意思是:create table 元数据(
      横向名称 varchar(50),
      纵向名称 varchar(50),
      值 varchar(100),
      primary key(横向名称,纵向名称))
      

  4.   

    好玩!楼主,给我写一个打散表记录到元数据的通用的insert语句出来好不好?!
      

  5.   

    哦不,一条不行。一条形成@exec,另一条 insert...exec(@exec)
      

  6.   

    w_rose(w_rose):
    楼主,给我写一个打散表记录到元数据的通用的insert语句出来好不好?!
    ==================================================================
    中午 我试试。
      

  7.   

    第三篇 join,group by 的非常规用法也已放出
    http://expert.csdn.net/Expert/TopicView1.asp?id=2609297