Oracle中行轉列以及Join小總結

來源:互聯網
上載者:User

在Oracle中行轉列,可以利用decode函數:

如有學生表A:

ID NAME SUBJECT SCORE
1 張三 語文 90
2 張三 數學 80
3 李四 語文 99
4 李四 數學 78
5 張三 英語 89

現要轉換成下表:

NAME 語文 數學 英語
張三 90 80 89
李四 99 78  

這是一個典型的行轉列過程,只需如下SQL即可:

select NAME, sum(decode(SUBJECT,'語文', SCORE, null)), sum(decode(SUBJECT,'數學', SCORE, null)), sum(decode(SUBJECT,'英語', SCORE, null)) from A  group by NAME;


關於Join:

1.inner join :

select * from A a, B b where a.id = b.aId 與

select * from A a inner join B b on a.id = b.aId 是一樣的;

2. left join 與 left outer join:

select * from A a left join B b on (a.id = b.aId);

此時,不管B中有沒有對應A的記錄,都會查出A表中的所有記錄;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.