Perspective transformation table

The ranks of the inversion table display (perspective transformation)

1) Create a data table and add data

create table Score
(
    学号 nvarchar(10)
    Curriculum nvarchar ( 10 )
    , Results nvarchar ( 10 )
);
INSERT  INTO Score (student number, course, score) values ( ' 0001 ' , ' Language ' , 87 ), ( ' 0001 ' , ' mathematics ' , 79 ), ( ' 0001 ' , ' English ' , 95 )
, ( ' 0002 ' , ' language ' , 69 ), ( ' 0002 ' , ' mathematics ' , 84 );

2), the first query to observe the entire structure of the table

select * from Score;

 

 3), the first display to show the basic structure of

select student number, ' language ' , ' mathematics ' , ' English '   from Score;

 

 4), using a case statement if the course is directly displayed language = language performance, 0 otherwise, with other subjects

select Student ID
    , Case  the when courses = ' language '  the then score the else  0  End  AS ' language ' 
    , Case  the when courses = ' math '  the then score the else  0  End  AS ' mathematics ' 
    , Case  the when courses = ' English '  the then score the else  0  End  AS ' English ' 
from Score;

 

 5), line 5 from line 2 becomes, using packet aggregation (by Student packet number, packet aggregation subjects)

select Student ID
    , SUM ( Case  the when courses = ' language '  the then score the else  0  End ) AS ' language ' 
    , SUM ( Case  the when courses = ' math '  the then score the else  0  End ) AS ' mathematics ' 
    , SUM ( Case  the when courses = ' English '  then scores the else  0  End) AS ' English ' 
from Score Group  by student number;

Guess you like

Origin www.cnblogs.com/licm/p/12080940.html