博客
关于我
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命令:set sql_log_bin=on/off
查看>>
mySQL和Hive的区别
查看>>
MySQL和Java数据类型对应
查看>>
mysql和oorcale日期区间查询【含左右区间问题】
查看>>
MYSQL和ORACLE的一些操作区别
查看>>
mysql和redis之间互相备份
查看>>
MySQL和SQL入门
查看>>
mysql在centos下用命令批量导入报错_Variable ‘character_set_client‘ can‘t be set to the value of ‘---linux工作笔记042
查看>>
Mysql在Linux运行时新增配置文件提示:World-wrirable config file ‘/etc/mysql/conf.d/my.cnf‘ is ignored 权限过高导致
查看>>
Mysql在Windows上离线安装与配置
查看>>
MySQL在渗透测试中的应用
查看>>
Mysql在离线安装时启动失败:mysql服务无法启动,服务没有报告任何错误
查看>>
Mysql在离线安装时提示:error: Found option without preceding group in config file
查看>>
MySQL基于SSL的主从复制
查看>>
Mysql基本操作
查看>>
mysql基本操作
查看>>
mysql基本知识点梳理和查询优化
查看>>
mysql基础
查看>>
Mysql基础 —— 数据基础操作
查看>>
mysql基础---mysql查询机制
查看>>