- 爱易网页
-
数据库教程
- 在数据库中怎么让列变成行,让行变成列的有关问题
日期:2014-05-16 浏览次数:20430 次
在数据库中如何让列变成行,让行变成列的问题
- create database arron
- go
- use arron
- go
- -- createTable init Data
- create table students (
- name varchar(25),
- class varchar(25),
- grade int
- )
- insert into students values ('张三','语文',20)
- insert into students values ('张三','数学',90)
- insert into students values ('张三','英语',50)
- insert into students values ('李四','语文',81)
- insert into students values ('李四','数学',60)
- insert into students values ('李四','英语',90)
-
- -- solution1
- select * from students
- pivot(
- max(grade)
- FOR [class] IN ([语文],[数学],[英语])
- ) AS pvt
-
- -- solution2 相当于自连接
-
- select A.Name,A.grade as 语文,B.grade as 数学,C.grade as 英语
- from students A,students B,students C