Column and column conversion in sqlserver2000 (from simple to complex)

Source: Internet
Author: User

Today, qq friends have a buddy who is in an interview and has a column-and-column conversion question. At that time, he was a bit blind. When he came back, he looked at some people in the group and shared them together.

 

Result:

 

After reading this, I know that this is a standard conversion problem. To be honest, I just know that I have never studied it carefully.

Write a simple

 

Code

Declare @ table
(
ID int,
Course varchar (10 ),
Point int
)

Insert into @ table
Select 1, 'China', 87
Union all
Select 1, 'mat', 98
Union all
Select 2, 'China', 54
Union all
Select 3, 'China', 97
Union all
Select 3, 'mat', 92
Union all
Select 4, 'mat', 86
Union all
Select 5, 'mat', 65
Union all
Select 6, 'China', 76

Select ID
, Max (case when course = 'mate' then Point else 0 end) as 'mate'
, Max (case when course = 'China' then Point else 0 end) as 'China'
From @ table group by ID

This can solve the above problems simply. However, the problem arises. I now know that it is a two-Course: 'mat' and 'China.

What if I don't know how many courses there are? (To be continued)

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.