SQL converts rows and columns, and SQL converts rows and columns.

Source: Internet
Author: User

SQL converts rows and columns, and SQL converts rows and columns.


Name Subject Score
Niufen Chinese 81
Niufen Mathematics 88
Niufen English 84
Zhang San Chinese 90
Zhang San Mathematics 98
Zhang San English 90

(Table 1)

An existing table, such as (Table 1)

Name Chinese Mathematics English
Niufen 81 88 84
Zhang San 90 98 90
(Table 2)

To convert to (table 2)

SQL: select stuName as name, chinese as language, math as mathematics, english as english from (

Select sutName,

Case subject when chinese then score end as chinese,

Case subject when math then score end as math,

Case subject when english then score end as english

From table) as tmp

Group by stuName


To convert (table 2) to (Table 1)

There is an unnest function in postgresql that can be used:

Select stuName, unnest (array ['China', 'Math', 'inc']) as subject, unnest (array [chinest, math, englist]) as score from table group by stuName

That's simple.


SQL row/column Conversion

First:
Select [name] = max ([name]), mathematics = sum (case when [course] = 'mate' then [score] else 0 end ), physical = sum (case when [course] = 'physical 'then [score] else 0 end ), english = sum (case when [course] = 'English 'then [score] else 0 end ), language = sum (case when [course] = 'China' then [score] else 0 end) from tb group by [name]
Second:
Declare @ SQL varchar (8000)
Set @ SQL =''
Select @ SQL = @ SQL + [course] + '= sum (case when [course] = ''' + [course] + ''' then [score] else 0 end), 'From (select distinct [course] from TB)
Set @ SQL = left (@ SQL, len (@ SQL)-1)
Set @ SQL = 'select [name] = max ([name]), '+ @ SQL +' from tb group by [name]'
Exec (@ SQL)

Urgent: SQL statement for Row/column Conversion

Your changes seem meaningless. Maybe you want to change the current situation.
* ***** Original vertical table tb *****
Student ID, subject score
1001 Chinese 90
1001 mathematics 80
1001 English 70
1002 Language 95
1002 mathematics 85
1002 English 75

* ***** Target horizontal table *****
Student ID: total score of Chinese mathematics and English
1001 90 80 70 240
1002 95 85 75 255

SQL statement:
Select student ID as 'student ID ',
Sum (case subject when 'China' then score else 0 end) as 'China ',
Sum (case subject when 'mate' then score else 0 end) as 'mate ',
Sum (case subject when 'then score else 0 end) as 'English ',
Sum (case when subject! = ''Then score end) as 'total score'
From tb
Group by student ID

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.