[SQL statement] Normal row-column conversion (Full Version)
Source: Internet
Author: User
Title: normal row/column conversion (Version 2.0)Note: normal row/column conversion (version 1.0) only provides static and dynamic writing for SQL Server 2000, and Version 2.0 adds SQL Server 2005 related writing.Question: Suppose there is a student orders table (TB) as follows:Name course scoreZhang San Language 74James math 83Zhang San physical 93Li Si language 74Li Si mathematics 84Li Si physical 94(The following result is displayed ):Name, Chinese, Mathematics, Physics----------------Li Si 74 84 94Zhang San 74 83 93-------------------*/Create Table Tb (name varchar (10), course varchar (10), scoreInt)Insert into TB values ('Zhang san','China', 74)Insert into TB values ('Zhang san','Mat', 83)Insert into TB values ('Zhang san','Physical', 93)Insert into TB values ('Li si','China', 74)Insert into TB values ('Li si','Mat', 84)Insert into TB values ('Li si','Physical', 94)Go-- SQL Server 2000 static SQL indicates that the course only includes three courses: Chinese, mathematics, and physics. (Same as below)Select nameAsName,Max (CaseCourse when'China'Then scoreElse0 end) language,Max (CaseCourse when'Mat'Then scoreElse0 end) mathematics,Max (CaseCourse when'Physical'Then scoreElse0 end) PhysicalFrom TBGroup by name-- SQL Server 2000 dynamic SQL refers to three courses, including Chinese, mathematics, and physics. (Same as below)Declare @ SQL varchar (8000)Set@ SQL ='Select name'Select @ SQL = @ SQL +', Max (Case course when'''+ Course +'''Then score else 0 end )['+ Course +']'From (select distinct course from TB)AsASet@ SQL = @ SQL +'From TB group byname'Exec (@ SQL)-- SQL Server 2005 static SQL.Select * from (select * from TB) a round (max (score)ForCourseIn(Chinese, Mathematics, Physics) B-- SQL Server 2005 dynamic SQL.Declare @ SQL varchar (8000)Select @ SQL = isnull (@ SQL +'], [','') + Course from TB group by courseSet@ SQL ='['+ @ SQL +']'Exec ('Select * from (select * from TB) a round (max (score) for course in ('+ @ SQL +') B')---------------------------------/*Problem: Based on the above results, the average score and total score are added. The following result is obtained:Name, Chinese, mathematics, and physics average score--------------------------Li Si 74 84 94 84.00 252Zhang San 74 83 93 83.33 250*/-- SQL Server 2000 static SQL.Select name,Max (CaseCourse when'China'Then scoreElse0 end) language,Max (CaseCourse when'Mat'Then scoreElse0 end) mathematics,Max (CaseCourse when'Physical'Then scoreElse0 end) physical,Cast (AVG (score * 1.0)AsDecimal() Average score,Sum (score) total scoreFrom TBGroup by name-- SQL Server 2000 dynamic SQL.Declare @ SQL varchar (8000)Set@ SQL ='Select name'Select @ SQL = @ SQL +', Max (Case course when'''+ Course +'''Then score else 0 end )['+ Course +']'From (select distinct course from TB)AsASet@ SQL = @ SQL +', Cast (AVG (score * 1.0) as decimal () average score, sum (score) total score from TB group by name'Exec (@ SQL)-- SQL Server 2005 static SQL.Select M. *, N. Average score, N. Total score from(Select * from TB) a round (max (score)ForCourseIn(Chinese, Mathematics, Physics) B) m,(Select name, cast (AVG (score * 1.0)AsDecimal() Average score, sum (score) total score from TB group by name) NWhere M. Name = n. Name-- SQL Server 2005 dynamic SQL.Declare @ SQL varchar (8000)Select @ SQL = isnull (@ SQL +',','') + Course from TB group by courseExec ('select M. *, N. Average score, N. Total score from(Select * from TB) a round (max (score)ForCourseIn('+ @ SQL +') B) m,(Select name, cast (AVG (score * 1.0)AsDecimal() Average score, sum (score) total score from TB group by name) NWhere M. Name = n. name ')Drop table TB------------------------------------/*Question: If the two tables change each other: the table structure and data are:Name, Chinese, Mathematics, PhysicsZhang San 74 83 93Li Si 74 84 94(The following result is displayed ):Name course score------------Li Si language 74Li Si mathematics 84Li Si physical 94Zhang San Language 74James math 83Zhang San physical 93--------------*/Create Table Tb (name varchar (10), ChineseInt, MathematicsInt, PhysicalInt)Insert into TB values ('Zhang san', 93)Insert into TB values ('Li si', 94)Go-- SQL Server 2000 static SQL.Select * from(Select name, course ='China', Score = Chinese from TBUnion allSelect name, course ='Mat', Score = mathematics from TBUnion allSelect name, course ='Physical', Score = physical from TB) TOrder by name,CaseCourse when'China'Then 1 when'Mat'Then 2 when'Physical'Then 3 end-- SQL Server 2000 dynamic SQL.-- Call the dynamic ecosystem of the system table.Declare @ SQL varchar (8000)Select @ SQL = isnull (@ SQL +'Union all','') +'Select name, [course] ='+ Quotename (name,'''') +', [Score] ='+ Quotename (name) +'From tb'From syscolumnsWhere name! = N'Name'And Id = object_id ('Tb') -- Table name TB, excluding other columns whose names areOrder by colid ASCExec (@ SQL +'Order by name')-- SQL Server 2005 dynamic SQL.Select name, course, score from TB unordered (scoreForCourseIn([Chinese], [mathematics], [physics]) T-- SQL Server 2005 dynamic SQL, same as SQL Server 2000 dynamic SQL.--------------------/*Problem: add an average score and the total score to the above result. The following result is obtained:Name course score----------------Li Si language 74.00Li Si, mathematics 84.00Li Si physical 94.00Li Si average score 84.00Li Si's total score is 252.00Zhang San Chinese 74.00Zhang San, mathematics 83.00Zhang San physical 93.00Michael Jacob has an average score of 83.33.Zhang San's total score is 250.00------------------*/Select * from(Select nameAsName, course ='China', Score = Chinese from TBUnion allSelect nameAsName, course ='Mat', Score = mathematics from TBUnion allSelect nameAsName, course ='Physical', Score = physical from TBUnion allSelect nameAsName, course ='Average', Score = cast (Chinese + mathematics + physics) * 1.0/3AsDecimal(18, 2) from TBUnion allSelect nameAsName, course ='Total', Score = Chinese + mathematics + Physical from TB) TOrder by name,CaseCourse when'China'Then 1 when'Mat'Then 2 when'Physical'Then 3 when'Average'Then 4 when'Total'Then 5 endTurn: http://www.cnblogs.com/temptation/archive/2012/04/02/2429904.html
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.