Sqlselect-row-column Conversion

Source: Internet
Author: User

Normal row/column Conversion

Suppose there is a student renewal table (T) as follows:

Name subject result
Zhang San Chinese 73
James math 83
Zhang San physical 93
Li Si language 74
Li Si mathematics 84
Li Si physical 94

Want to become
Name, Chinese, Mathematics, Physics
Zhang San 73 83 93
Li Si 74 84 94

Create Table # T
(
Name varchar (10 ),
Subject varchar (10 ),
Result int
)

Insert into # T (name, subject, result) values ('zhang san', 'China', '73 ')
Insert into # T (name, subject, result) values ('zhang san', 'mat', '83 ')
Insert into # T (name, subject, result) values ('zhang san', 'Physical ', '93 ')
Insert into # T (name, subject, result) values ('Li si', 'China', '74 ')
Insert into # T (name, subject, result) values ('lily', 'mat', '83 ')
Insert into # T (name, subject, result) values ('lily', 'Physical ', '93 ')

Declare @ SQL varchar (8000)
Set @ SQL = 'select name as '+ 'name'
Select @ SQL = @ SQL + ', sum (case subject when ''' + Subject + ''' then result end) [' + Subject + ']'
From (select distinct subject from # T) as
Set @ SQL = @ SQL + 'from # T group by name'
Exec (@ SQL)

Drop table # T

-- Result
Name, mathematics, physics, and Chinese
-------------------------------------------
Li Si 83 93 74
Zhang San 83 93 73
 
----------------------------------------------------
If the two tables change each other:

Name, Chinese, Mathematics, Physics
Zhang San 73 83 93
Li Si 74 84 94

Want to become
Name subject result
Zhang San Chinese 73
James math 83
Zhang San physical 93
Li Si language 74
Li Si mathematics 84
Li Si physical 94

Create Table # T
(
Name varchar (10 ),
Chinese int,
Mathematical int,
Physical int
)

Insert into # T (name, Chinese, Mathematics, Physics) values ('zhang san', 93)
Insert into # T (name, Chinese, mathematics, and physics) values ('Li si', 94)

Select name as name, 'China' as subject, language as result from # T Union
Select name as name, 'mate' as subject, mathematics as result from # T Union
Select name as name, 'Physical 'as subject, physical as result from # T
Order by name DESC

Drop table # T
 
-- Result
Name subject result
----------------------------
James math 83
Zhang San physical 93
Zhang San Chinese 73
Li Si mathematics 84
Li Si physical 94
Li Si language 74

(The number of affected rows is 6)

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.