SQL Scale Table Conversion

Source: Internet
Author: User

Horizontal Table Conversion Vertical table:

CREATE Table score (username varchar (ten), Chinese int, math int, physics int); INSERT into score (username , Chinese, math, physics) VALUES (' Kay ', 74,83,93), INSERT into score (username, Chinese, math, physics) VALUES (' Jim ', 75, 84,94); INSERT into score (username, Chinese, math, physics) VALUES (' Lily ', 86,49,91); COMMIT; SELECT * from score;

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/59/2A/wKioL1TJmWTDimcKAABhyIsoWbg511.jpg "title=" score. PNG "alt=" Wkiol1tjmwtdimckaabhyisowbg511.jpg "/>

The key to converting a horizontal table to a vertical table is how to convert a record into multiple records, three sub-fields corresponding to three records, a single record with only three records in the intermediate table and a score table as a Cartesian product to copy a record into three records

With T as (select ' Chinese ' course from dual UNION ALL select ' Math ' course from dual UNION all select ' Physics ' C Ourse from dual) SELECT Username,course,decode (course, ' Chinese ', Chinese, ' math ', Math, ' physics ', physics) from score, T O Rder by username, course;

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/59/2D/wKiom1TJmVmAwnwpAADqXvmhWJg275.jpg "title=" T.png " alt= "Wkiom1tjmvmawnwpaadqxvmhwjg275.jpg"/>


This article from the "Down-to-earth, look at the Stars" blog, please be sure to keep this source http://xubcing.blog.51cto.com/3502962/1609478

SQL Scale Table Conversion

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.