Assume that there are four subjects in analyticdb.
Add a data entry for each subject
Create Table [DBO]. [score] (
[ID] [int] identity (1, 1) not null,
[Name] [varchar] (50) Collate chinese_prc_ci_as null,
[Subject] [varchar] (50) Collate chinese_prc_ci_as null,
[Score] [int] Null
) On [primary]
Let's take a look at this.
Declare @ A varchar (2000)
Set @ A =''
Select @ A = @ A + ',' + Cast (ID as varchar) from Score
Print @
Select name,
Sum (case subject when' then score else 0 end) as English,
Sum (case subject when 'physical 'Then score else 0 end) as physical,
Sum (case subject when 'mate' then score else 0 end) as mathematics,
Sum (case subject when 'Chemical' then score else 0 end) as chemistry
From score group by name
Note: When the field is placed after the when, multiple conditions can be judged.
Sum (case when subject <> 'English 'Then score else 0 end) as not English,