SQL row-to-Column

Source: Internet
Author: User
Create   Table ABC
(
Student Varchar ( 50 ),
Class Varchar ( 50 ),
Grade Int
)
Insert   Into ABC
Select   ' Sun Xiaomei ' , ' Mathematics ' , 10   Union   All
Select   ' Sun Xiaomei ' , ' Chinese ' , 20   Union   All
Select   ' Sun Xiaomei ' , ' English ' , 30   Union   All
Select   ' Tuber ' , ' Mathematics ' , 40   Union   All
Select   ' Tuber ' , ' Chinese ' , 50   Union   All
Select   ' Tuber ' , ' English ' , 60   Union   All
Select   ' Xiao Ding ' , ' Mathematics ' , 70   Union   All
Select   ' Xiao Ding ' , ' Chinese ' , 80   Union   All
Select   ' Xiao Ding ' , ' English ' , 90

Select  
Student,
Max (Mathematics) As Mathematics,
Max (Chinese) As Chinese,
Max (English) As English
From
(
Select  
Student,
Case Class When   ' Mathematics '   Then Grade End   As Mathematics,
Case Class When   ' Chinese '   Then Grade End   As Chinese,
Case Class When   ' English '   Then Grade End   As English
From ABC
) As A
Group   By Student /**/ /*
Code Amount of benefit for pharmaceutical products of the Purchasing Unit
01 Guangzhou Western medicine 10
01 Guangzhou Chinese patent 9
01 Guangzhou Medical Materials 8
01 Guangzhou herbal medicine 7
02 Xi'an Western medicine 2
02 Xi'an medical materials 6
03 Shenzhen Western Medicine 5
*/
Select Code, purchase unit,
" Western medicine " = (Select give profits from ABC where = ' Western medicine ' And code = T1. Code ),
" Chinese Patent Medicine " = (Select give profits from ABC where = ' Chinese Patent Medicine ' And code = T1. Code ),
" Medical Materials " = (Select give profits from ABC where = ' Medical Materials ' And code = T1. Code ),
" Chinese herbal medicine " = (Select give profits from ABC where = ' Chinese herbal medicine ' And code = T1. Code ),
" Total " = (Select sum (pharmaceutical) from ABC where code = T1. code group by code)
From ABC T1 group by code, purchase Unit
/**/ /*
Code purchase unit Western medicine Chinese patent medicine Chinese herbal medicine total
01 Guangzhou 10 9 8 7 34
02 Xi'an 2 0 6 0 8
*/

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.