Oracle row-to-column (pivot, WM_CONCAT, decode) Usage Summary

Source: Internet
Author: User

CREATE TABLE CC
(Student NVARCHAR2 (2), Course NVARCHAR2 (2), score INT
);

INSERT into CC
Select n ' Zhang San ', N ' language ', dual union ALL
Select N ' Zhang San ', N ' math ', the dual union ALL
SE Lect N ' Zhang San ', N ' English ', from dual UNION ALL
Select N ' Zhang San ', N ' Physics ', all-from-dual UNION ALL
Select n ' John Doe ', n ' language ', from dual UNION ALL
Select n ' John Doe ', n ' math ', dual from the union ALL
Select n ' John Doe ', n ' English ', all in dual union ALL
Select n ' John Doe ', N ' objects The dual;
Commit;

/*
--Want to see results:
Li 477 292
Zhang 387 337 1 decode Method
SELECT
Student, Br>max (DECODE (COURSE, ' language ', score)) A,
Max (DECODE (COURSE, ' math ', score)) B,
MAX (DECODE (COURSE, ' English ', score)) C,
max (DECODE (COURSE, ' physical ', score)) D,
SUM (Score) Total
from
CC
GROUP by
Student;

--2 using the Wm_concat method
SELECT Student,wm_concat (Score), SUM (score) from the CC GROUP by STUDENT;
--3 using the Oracle 11g Pivot method
SELECT kin.*, Kin.a + kin.b + kin.c + KIN.D as Total
From (SELECT *
From CC PIVOT (MAX (score) to COURSE in (' Language ' as A,
' Math ' as B,
' English ' as C,
' Physical ' as D ')) KIN;

Transferred from: http://blog.csdn.net/jxzkin/article/details/7949629

Oracle row-to-column (pivot, WM_CONCAT, decode) Usage Summary

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.