SQL writes a table-and-chart conversion

Source: Internet
Author: User

The basic idea of converting a horizontal table to a vertical table is:

  1) The horizontal table of multiple data, "pressure" into one. The equivalent of grouping so many, each group "presses" into one piece of data. Using GROUP BY

2) The columns in the vertical table are populated with the data for a particular row, using the value end that is not used in case of ' a ' then ' B ' else, in order to filter some unused data, the commonly used function has Max (), SUM ()

PS: If there is no aggregation function handsome election. The database will return the first data to you, basically just you fill out "No use value"

Here is an example:

SELECTNAME,MAX( CaseSUBJECT when 'language'  ThenResultELSE 0 END) languages,MAX( CaseSUBJECT when 'Mathematics'  ThenResultELSE 0 END) Mathematics,MAX( CaseSUBJECT when 'Physical'  ThenResultELSE 0 END) Physical fromTBGROUP  by' Name '

Get the result is

SQL writes a table-and-chart 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.