Table transpose in Access and SQL Server

Source: Internet
Author: User

The sample table is as follows:

Student
ID SName Subject score
1 Tom Chinese 80
2 Tom Math 90
3 Tom 中文版 85
4 John doe Chinese 85
5 John doe Math 92
6 John doe 中文版 82

Access:

TRANSFORM AVG (student.score) as Scoreofavg
SELECT Student.sname
From Student
GROUP by Student.sname
PIVOT Student.subject;

  

SQL Server:

Select SName,
AVG (case when subject= ' Chinese "then score end) as ' Chinese ',
AVG (case when subject= ' 中文版 ' then score end) as ' 中文版 ',
AVG (case if subject= ' math ' then score end) as ' math '
From Student
Group by SName

 
SName Chinese 中文版 Math
John doe 85 82 92
Tom 80 85 90


Upgrade version:

DECLARE @s varchar (8000), @l varchar (8000)
Set @s= "
Select @[email protected] + ', AVG (case Subject when "+ subject+" then score else 0 end) as [' +subject+ '] ' from Student Group by Subject
Set @l= ' Select sName ' [email protected]+ ' from Student Group by SName ORDER by SName '
Exec (@l)

  

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.