SQL Server case when usage Summary

Source: Internet
Author: User

First, create a table and insert a data statement:

Use Student

Go

Create table Score

(

Student ID nvarchar (10 ),

Nvarchar (10 ),

Score int

)

Go

Insert into Score values ('123', 'China', 87 );

Insert into Score values ('123', 'mat', 79 );

Insert into Score values ('123', 'English ', 95 );

Insert into Score values ('123', 'China', 69 );

Insert into Score values ('123', 'mat', 84 );

Insert into Score values ('123', 'China', 95 );

Case when usage 1:

A case simple expression that compares an expression with a group of simple expressions to determine the result.

Select student ID,

Sum (case when course = 'China' then score else 0 end) as language,

Sum (case when course = 'mate' then score else 0 end) as mathematics,

Sum (case when course = 'English 'then score else 0 end) as English

From Score

Group by student ID

Case when usage 2:

A case search expression that computes a set of boolean expressions to determine the result.

Select student ID, score,

Case score

When 87 then 'liang'

When 79 then 'liang'

When 95 then 'out'

When 69 then '中'

Else 'case' end as test

From Score

The above is my understanding of case when. If you have any mistakes and want to criticize them, thank you!

Note: Each case corresponds to a column of data.

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.