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:
- Let's get the language scores in this subject. Since we used the group by statement, here certainly use aggregate functions to seek score.
- 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:
Field case when value 1 then results when value 2 then result 2 ... the else default result end
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.
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
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.
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
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
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
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.