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.