博客
关于我
SQL Server 列转行的实现
阅读量:286 次
发布时间:2019-03-03

本文共 882 字,大约阅读时间需要 2 分钟。

不管我们在平时的学习或工作中,难免会遇到列转行的数据操作,下面的例子可参考一下

1、我们先建表

drop table if exists stu_Score create table stu_Score(name varchar(10),java INT,C# INT,python INT)insert into stu_Score VALUES('Dina',82,93,90)insert into stu_Score VALUES('Joyce',87,80,95)insert into stu_Score VALUES('Mandy',93,86,90)

2、看一下 stu_Score 表 的数据

select * from stu_Score

3、实现数据的列转行

方法一:

select * from(   select name,course='java',score=java from stu_Score    union all  select name,course='C#',score=C# from stu_Score    union all  select name,course='python',score=python from stu_Score)stu_Infoorder by name, --下面是为了按照jave、C#、python 的格式输出case course 	when 'java' then 1 	when 'C#' then 2 	when 'python' then 3 end

方法二:

select name,cource,score from stu_Score unpivot (score for cource in([java],[C#],[python]))stu_Info

 两种方法 查询出来的数据都是一样的,可见下图:

希望对你有帮助!!! 

若想了解SQL Server 的行转列实现小示例,可点击 

若想了解SQL Server 的列转行 与 行转列 的综合应用小示例,可点击 

转载地址:http://iwpl.baihongyu.com/

你可能感兴趣的文章
MySQL - 解读MySQL事务与锁机制
查看>>
MTTR、MTBF、MTTF的大白话理解
查看>>
mt_rand
查看>>
mysql -存储过程
查看>>
mysql /*! 50100 ... */ 条件编译
查看>>
mudbox卸载/完美解决安装失败/如何彻底卸载清除干净mudbox各种残留注册表和文件的方法...
查看>>
mysql 1264_关于mysql 出现 1264 Out of range value for column 错误的解决办法
查看>>
mysql 1593_Linux高可用(HA)之MySQL主从复制中出现1593错误码的低级错误
查看>>
mysql 5.6 修改端口_mysql5.6.24怎么修改端口号
查看>>
MySQL 8.0 恢复孤立文件每表ibd文件
查看>>
MySQL 8.0开始Group by不再排序
查看>>
mysql ansi nulls_SET ANSI_NULLS ON SET QUOTED_IDENTIFIER ON 什么意思
查看>>
multi swiper bug solution
查看>>
MySQL Binlog 日志监听与 Spring 集成实战
查看>>
MySQL binlog三种模式
查看>>
multi-angle cosine and sines
查看>>
Mysql Can't connect to MySQL server
查看>>
mysql case when 乱码_Mysql CASE WHEN 用法
查看>>
Multicast1
查看>>
mysql client library_MySQL数据库之zabbix3.x安装出现“configure: error: Not found mysqlclient library”的解决办法...
查看>>