SQL Server row to column languages, mathematics, physics, Chemistry

Source: Internet
Author: User

Database Query row goto column

1. Original database value

stdname    stdsubject    result Zhang San          Chinese          Zhang San          math          Zhang San          Physical          - John Doe          Chinese          John Doe          Mathematics          John Doe          Physics          the John Doe          chemistry          John Doe          chemistry         

2. To obtain the following table

stdname    Chinese    Mathematics    Physics    Chemistry John Doe                        164  Zhang San                        NULL

3. Implementation methods

--first call row to column functionSelect *  from(SelectStdname,stdsubject,result fromTEST1) asa pivot (sum(Result) forStdsubjectinch(Chinese, mathematics, Physics, Chemistry)) T--The second kind of writing yourselfSelectStdname, Chinese=(Select sum(Result) fromTest1whereStdname=A.stdname andStdsubject= 'language'), Mathematics=(Select sum(Result) fromTest1whereStdname=A.stdname andStdsubject= 'Mathematics'), Physical=(Select sum(Result) fromTest1whereStdname=A.stdname andStdsubject= 'Physical'), Chemical=(Select sum(Result) fromTest1whereStdname=A.stdname andStdsubject= 'Chemical' ) fromTest1 asaGroup  by Stdname 
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.