Sql ranks of (vertical and horizontal table) Conversion

Create a table scores

First, the traditional ranks of conversion

Table transverse vertical turn table

We want to turn into a cross-table is like this:

Since this table has only two, you can group by name. First name patchwork, behind the scores we need to.

select the name from scores group by name 

result:

analysis:

  1. Let's get the language scores in this subject. Since we used the group by statement, here certainly use aggregate functions to seek score.
  2. And we just need to score the language of this Section, a packet out of a total of three, namely, language, mathematics, physics. Then you need to determine subjects to get scores.

Here we meet the needs of the case statement on debut. It c # in the switch-case effect the same.

sql case statement syntax:

Copy the code
Field case 
    when value 1 then results 
    when value 2 then result 2 
    ... 
    the else default result 
end
Copy the code
select name, SUM (case course when 'language' then scores else 0 end) as the language from scores group by name 

result:

Since taking the score to language, and other subjects change conditions on it.

Complete sql:

select name, 
SUM (Case curriculum when 'language' then scores else 0 end) as language, 
SUM (Case curriculum when 'mathematics' then scores else 0 end) as mathematics, 
SUM (Case curriculum when 'physical' then scores else 0 end ) as a physical 
from scores group by name

Table longitudinal horizontal turn table

We first just turn a good table, insert a new table in Scores2.

Copy the code
select name, 
SUM (Case curriculum when 'language' then scores else 0 end) as language, 
SUM (Case curriculum when 'mathematics' then scores else 0 end) as mathematics, 
SUM (Case curriculum when 'physical' then scores else 0 end ) as physical 
INTO scores2 
from Scores by Group name
Copy the code

We also put Joe Smith and John Doe language scores to check out.

select name, 
 'language' as curriculum, 
 language as scores 
 from scores2

result:

There are two subjects how to do data? Quite simply, we have found out one by one, and then use the union all their combinations as a table on it.

Copy the code
select name, 
 'language' as curriculum, 
 language as scores 
 from scores2 
 of Union All 
  select name, 
 'mathematics' as curriculum, 
 mathematics as scores 
 from scores2 
 of Union All 
  select name, 
 'physical' as curriculum, 
 physical as scores 
 from scores2 
 the Order by name desc
Copy the code

result:

But I have not felt a lot of trouble it? Do not worry, we have a simpler way. Here we will introduce pivot relational operators.

sql server 2005 pivot is provided by operators, it can be used as long as a database of more than 05 versions. Mainly used to convert rows and columns.

 Table horizontal longitudinal pivot turn table

Copy the code
the SELECT 
    T2. Name, 
    T2. mathematics, 
    T2. physical, 
    T2. language 
from AS Scores T1 
Pivot (SUM (score) for courses in (math, language, physical)) as t2
Copy the code

The original data row pivot table field of mathematics course, language, physical conversion into a column, and the column takes a value corresponding to a sum.

We only need to remember it is used on it.

Table longitudinal horizontal turn table unpivot

the SELECT 
     * 
 from 
 scores2 
 UNPIVOT (points for courses in (language, mathematics, physics)) as t3

 unpivot the language, mathematics, physical columns into rows, a fraction of the new deposit corresponding value.

Guess you like

Origin www.cnblogs.com/approx/p/11763465.html