The horizontal table of the database and the longitudinal table to the horizontal table

Source: Internet
Author: User

/* Horizontal table variable longitudinal table or Vertical table variable horizontal table

Score table below
Name Course Grade
Zhang San language 95
Zhang San Mathematics 90
Zhang San English 89
John Doe Language 92
John Doe Mathematics 88
John Doe English 97
Become

Name Chinese maths English
Sheet 395 90 89
Lee 492 88 97
*/

--The  longitudinal table to horizontal table CREATE TABLE Biao (PName varchar (Ten), Pcourse varchar (Ten), Pgradeint) go insert INTO Biao values ('Zhang San','language',' the') insert into Biao values ('Zhang San','Mathematics',' -') insert into Biao values ('Zhang San','English',' the') insert into Biao values ('John Doe','language',' the') insert into Biao values ('John Doe','Mathematics',' the') insert into Biao values ('John Doe','English',' the')   Select* fromBiao--Enter the conversion mode belowSelectPName, SUM ( CasePcourse when'language'Then PgradeElse 0End aslanguage, SUM ( CasePcourse when'Mathematics'Then PgradeElse 0End asMath, SUM ( CasePcourse when'English'Then PgradeElse 0End asEnglish fromBiao GROUP BY PName

-- horizontal table to longitudinal table --Example 2 creating table 2 CREATE TABLE BIAO1 (name varchar ( -), Languageint, Mathematicsint, Englishint) go insert INTO BIAO1 values ('Zhang San', the, -, the) insert into BIAO1 values ('John Doe', the, the, the) Select* fromBiao1--Enter the conversion mode belowSelect* fromBiao1SelectName'language'  asCourses, Languages asResults fromBIAO1 Union AllSelectName'Mathematics' asCourses, Mathematics asResults fromBIAO1 Union AllSelectName'English' asCourses, English asResults frombiao1 ORDER BY name Desc

The horizontal table of the database and the longitudinal table to the horizontal table

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.