如何根据一个关键字将多行数据合并一行显示
例如将
user_id,month,info1,info2
test1,8,test1info18,test1info28
test2,8,test2info18,test2info28
test1,9,test1info19,test1info29
test2,9,test2info19,test2info29
合并为
user_id,info1_8,info2_8,info1_9,info2_9
test1,test1info18,test1info28,test1info19,test1info29
test2,test2info18,test2info28,test2info19,test2info29

解决方案 »

  1.   

    如果都是number型的话用sum(case when ...)...group by...就能搞定,
    可惜是varchar2型的,不会整了
      

  2.   

    select user_id,
    max(decode(month,8,info1)) info1_8,
    max(decode(month,8,info2)) info2_8,
    max(decode(month,9,info1)) info1_9,
    max(decode(month,9,info2)) info2_9
    from table
    group by user_id
      

  3.   

    xinyou(我想当网管) 的思路是对的,就是Decode函数少了一个默认值吧。
      

  4.   

    Select user_id,
           (Select info1 From test a Where a.Month =8 And user_id = 'test1') into1_8,
           (Select info2 From test a Where a.Month =8 And user_id = 'test2') into2_8,
           (Select info1 From test a Where a.Month =9 And user_id = 'test1') into1_9,
           (Select info2 From test a Where a.Month =9 And user_id = 'test2') into2_9
      From test
      Group By user_id
      

  5.   

    Decode没有设默认值时的默认值是空