Summary of case usage in SQL Server

Source: Internet
Author: User

First build the table and insert the data statement:

Use Student

Go

CREATE TABLE Score

(

School number nvarchar (10),

Course nvarchar (10),

Score int

)

Go

INSERT into Score values (' 0001 ', ' language ', 87);

INSERT into Score values (' 0001 ', ' math ', 79);

INSERT into Score values (' 0001 ', ' English ', 95);

INSERT into Score values (' 0002 ', ' language ', 69);

INSERT into Score values (' 0002 ', ' math ', 84);

INSERT into Score values (' 0001 ', ' language ', 95);

Case when usage one:

Case simple expression that determines the result by comparing the expression to a simple set of expressions.

Select School Number,

SUM (case when course = ' language ' then accomplishment else 0 end) as language,

SUM (case when course = ' math ' then result else 0 end) as mathematics,

SUM (case when course = ' English ' then result else 0 end) as English

From Score

Group BY School Number

Case when usage two:

Case search expression that determines the result by calculating a set of Boolean expressions.

Select study number, score,

Case results

When then ' Liang '

When then ' Liang '

When then ' excellent '

When then ' in '

Else ' bad ' end as test

From Score

Above for my understanding of case, if there is a mistake I hope to criticize, thank you!

Note: Each case corresponds to a column of data

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.