将表:
ID   字段    值(字符串型)
-----------------------
A    高度    1
A    长度    2
A    宽度    3
B    高度    4
B    长度    5
B    宽度    6转换成如下:ID   高度    长度    宽度
-----------------------
A    1       2      3
B    4       5      6
请高手指教!谢谢!

解决方案 »

  1.   

    主要应用case语句来解决行转列的问题行转列问题主要分为两类1)简单的行转列问题:示例表:id  sid           course  result1   2005001 语文     80.0
    2   2005001 数学     90.0
    3   2005001 英语     80.0
    4   2005002 语文     56.0
    5   2005002 数学     69.0
    6   2005002 英语     89.0执行select sid,语文=isnull(sum(case course when '语文' then result end),0),
       数学=isnull(sum(case course when '数学' then result end),0),
       英语=isnull(sum(case course when '英语' then result end),0)
       from result 
       group by sid
       order by sid 得出结果sid           语文 数学 英语 2005001 80.0  90.0  80.0
    2005002 56.0  69.0  89.0 2)较为复杂的行转列表1:courseid name1 语文
    2 数学
    3 英语
    表2:resultid sid          course  result1 2005001 语文      80.0
    2 2005001 数学      90.0
    3 2005001 英语      80.0
    4 2005002 语文      56.0
    5 2005002 数学      69.0
    6 2005002 英语      89.0 declare @sql varchar(8000)
    set @sql='select sid'
    select @sql=@sql+','+course.name+'=isnull(sum(case course when '''+course.name+''' then result end),0)'
     from course order by id 
    set @sql=@sql+' from result group by sid order by sid'
    print @sql
    exec(@sql) 得出结果sid           语文 数学 英语 2005001 80.0  90.0  80.0
    2005002 56.0  69.0  89.0新闻来自: 新客网(www.xker.com) 详文参考:http://www.xker.com/page/e2007/0205/22415.html
      

  2.   

    select ID,
      max(case 字段 when '高度' then 值 else 0 end) 高度,
      max(case 字段 when '长度' then 值 else 0 end) 长度,
      max(case 字段 when '宽度' then 值 else 0 end) 宽度
    from tb
    group by ID
      

  3.   

    感谢楼上的,但是其实我知道这个方法,但是你例子里面那个要聚合的是实数型的,可以使用sum()函数,但是我要聚合的字符型。oracle没有提供对字符型的聚合函数,百度一下说要用自定义聚合函数,我想知道不使用的话,行不行。
      

  4.   


    select 
    id, (case when  字段='长度' then 值  else 0 end) as 长度,
        (case when  字段='宽度' then 值  else 0 end) as 宽度,
        (case when  字段='高度' then 值  else 0 end) as 高度
    from table group by id order by id
      

  5.   

    感谢2楼朋友,搞定了,哈哈。用max()函数,真高。