博客
关于我
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/

你可能感兴趣的文章
Nginx 学习总结(17)—— 8 个免费开源 Nginx 管理系统,轻松管理 Nginx 站点配置
查看>>
Nginx 学习(一):Nginx 下载和启动
查看>>
nginx 常用指令配置总结
查看>>
Nginx 常用配置清单
查看>>
nginx 常用配置记录
查看>>
nginx 开启ssl模块 [emerg] the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx
查看>>
Nginx 我们必须知道的那些事
查看>>
Nginx 源码完全注释(11)ngx_spinlock
查看>>
Nginx 的 proxy_pass 使用简介
查看>>
Nginx 的 SSL 模块安装
查看>>
Nginx 的优化思路,并解析网站防盗链
查看>>
Nginx 的配置文件中的 keepalive 介绍
查看>>
Nginx 相关介绍(Nginx是什么?能干嘛?)
查看>>
Nginx 知识点一网打尽:动静分离、压缩、缓存、跨域、高可用、性能优化...
查看>>
nginx 禁止以ip形式访问服务器
查看>>
NGINX 端口负载均衡
查看>>
Nginx 结合 consul 实现动态负载均衡
查看>>
Nginx 负载均衡与权重配置解析
查看>>
Nginx 负载均衡详解
查看>>