oracle行變列(多行變成一行/多行合并成一行/多行合并成多列/合并行)____oracle

來源:互聯網
上載者:User

和mysql對比著看:http://blog.csdn.net/rainyspring4540/article/details/50231435

這裡是oracle10g的環境

資料庫結構如下:


如果我想將相同名字的成績顯示在同一行,有2中基本展示:

第一種展示(顯示在同一行的不同列上)

其sql如下:

select name ,max(decode(type,'math',score,0)) as 數學,max(decode(type,'english',score,0)) as 英語,max(decode(type,'chinese',score,0)) as 語文from stu2group by name
由於這裡一個人只對應一科目的成績,所有你用max和sum是一樣的,如果一個人一個科目有多個成績,這時sum就是求和的意思了,而max就是取最大值,我就不在寫案例了,自己注意下就ok


第二中展示(顯示在同一行的同一列上)

sql如下:

select name ,max(decode(type,'math',score,0)) ||';'||max(decode(type,'english',score,0)) ||';'||max(decode(type,'chinese',score,0)) as allScorefrom stu2group by name
如果你熟悉字串拼接||的用法就能做出更多樣式:

樣式1:


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.