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