SQL Server 2000 row-to-column conversion implementation (horizontal)

Source: Internet
Author: User
Document directory
  • The result is:

[Reprinted]

 

 

In some statistical reports, row results are often displayed in columns. Here we use a common score report for each course of students to demonstrate the actual implementation method.

The table structure we use is as follows:

The relationship between the three tables is:

The existing test data is as follows:

The result is:

Declare @ strsql varchar (8000) set @ strsql = 'select T. stuname [name] 'select @ strsql = @ strsql + ', sum (case s. sname when ''' + sname + ''' then G. [score] End) ['+ sname +'] 'from (select sname from [subject]) as tmpselect @ strsql = @ strsql +' from [score] G, [subject] s, [Student] t where G. SID = S. sid and G. stuid = T. stuid group by T. stuid, T. stuname 'exec (@ strsql)

SQL Server 2005 has a built-in method to implement this function.

 

[I forgot my reprinted address]

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.