Implementation of SQL Server 2000 row-column (horizontal) _mssql

Source: Internet
Author: User

The table structure we use is as follows:

The relationship between the three sheets is:

The existing test data is:

The results we need are:


Copy Code code as follows:

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 SNA ME from [Subject]) as TMP
SELECT @strSQL = @strSQL + ' from [Score] g,[subject] s, [Student] t WHERE g.sid=s.sid and g.stuid = T.stuid GROUP by t.st UID, T.stuname '
EXEC (@strSQL)

In SQL SERVER 2005, there are already built-in ways to implement this functionality.

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.