SQL Row to Column

Source: Internet
Author: User
Tags getdate

1. Create a table and prepare the next data source

1 Create TableStudent2 (3Idint Primary Key Identity(1,1) not NULL,4Stunamenvarchar( -),5Coursenamenvarchar( -),6Scoreint,7CreatetimeDateTime 8 9 )Ten  One Insert  intoStudentValues('Zhang San','language',Bayi,GETDATE()) A Insert  intoStudentValues('Zhang San','Mathematics', the,GETDATE()) - Insert  intoStudentValues('Zhang San','English', the,GETDATE()) - Insert  intoStudentValues('John Doe','language', the,GETDATE()) the Insert  intoStudentValues('John Doe','Mathematics', the,GETDATE()) - Insert  intoStudentValues('John Doe','English', the,GETDATE())

2. When a case is implemented

1 SelectStuname,2  Max( CaseCoursename when 'language'  ThenScoreEnd) as 'language',3 Max( CaseCoursename when 'Mathematics'  ThenScoreEnd) as 'Mathematics',4 Max( CaseCoursename when 'English'  ThenScoreEnd) as 'English'5  fromStudentGroup  byStuname

3. Using the pivot function provided by Microsoft

SelectStuname,[language],[Mathematics],[English]  from ( SelectStuname,score,coursename fromStudent) asPpivot (sum(score) forCoursenameinch([language],[Mathematics],[English])) T

4. Record a function that adds a number of thousand characters to a digit

SELECT  ' 111111111 '  as CONVERT (VARCHAR(+),CAST(CONVERT(DECIMAL(2) ,LTRIM(111111111as Money),1)  dollar--lirim (field name)

SQL Row to Column

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.