SQL行转列
2015-11-20 17:05
253 查看
建表:
create table studentinfo(
name varchar(20) , subject varchar(20), score number(3,1));
插入记录:
insert into studentinfo values('张三','语文',69);
insert into studentinfo values('张三','数学',70);
insert into studentinfo values('李四','语文',20);
里面的记录为:
我们想要的结果为:
通过下面的sql可以实现:
select a 姓名,max(a1) 语文,max(a2) 数学 from ( select a, case when b = '语文' then c else null end as a1, case when b = '数学' then c else null end as a2 from (select name a,subject b,score c from studentinfo) x ) y group by a;
create table studentinfo(
name varchar(20) , subject varchar(20), score number(3,1));
插入记录:
insert into studentinfo values('张三','语文',69);
insert into studentinfo values('张三','数学',70);
insert into studentinfo values('李四','语文',20);
里面的记录为:
我们想要的结果为:
通过下面的sql可以实现:
select a 姓名,max(a1) 语文,max(a2) 数学 from ( select a, case when b = '语文' then c else null end as a1, case when b = '数学' then c else null end as a2 from (select name a,subject b,score c from studentinfo) x ) y group by a;
相关文章推荐
- HQL/QBC/Native SQL三种查询策略
- Mysql-DML简介(初学者用)
- ORACLE中DBMS_SQL的用法
- oracle中SET DEFINE意思
- mybatis调用存储过程
- 【转】使用JMeter对数据库做压力测试
- postgresql 客户端登录配置
- 【转】JMeter测试有无数据库连接池的性能
- ORACLE-SQL积累(查询外检约束、记录长度、字段长度、启用禁用触发器、case用法、分组连接函数、查询被锁表)
- MongoDB的C#驱动基本使用
- SQL行转列
- MYSQL-LOAD DATA 0row问题解决
- MySQL常用命令
- SQLSERVER 调用DLL 解密
- MongoDB数组或者对象数据查询
- MySQL记录存在则更新,不存在插入on duplicate key update
- iBATIS.net获取运行时sql语句
- mysql导出导入某张表
- mysql中int、bigint、smallint 和 tinyint的区别与长度
- 数据库 子查询和分页查询