Typical examples
First, row to column
1. Create a form
IF object_id ('TB') is not NULL DROP table tbgocreate table TB (name VARCHAR (Ten), Course VARCHAR (Ten), fractional INT) insert into TB VALUES ('Zhang San','language', About) insert INTO TB VALUES ('Zhang San','Mathematics', the) insert INTO TB VALUES ('Zhang San','Physical', the) insert INTO TB VALUES ('John Doe','language', About) insert INTO TB VALUES ('John Doe','Mathematics', -) insert INTO TB VALUES ('John Doe','Physical',94) Goselect*From tbgo
Name Course Score
---------- ---------- -----------
Zhang San language 74
Zhang San Mathematics 83
Zhang San Physics 93
John Doe Language 74
John Doe Mathematics 84
John Doe Physics 94
2. Using SQL Server 2000 static SQL
SELECT name, max (case course when' language '0 END) language, max (case coursewhen'
Math '0 END ' math, max (case coursewhen ' physical '0 END) physical from Tbgroup by name
3. Using SQL Server 2005 static SQL
SELECT * from TB Pivot (MAX (score) for course in (language, mathematics, physics)) a
SQL Column Career