SQL Server基础之行数据转换为列数据
作者:小子pk了 发布时间:2024-01-19 22:16:00
准备工作
创建表
use [test1]
go
create table [dbo].[student](
[id] [int] identity(1,1) not null,
[name] [nvarchar](50) null,
[project] [nvarchar](50) null,
[score] [int] null,
constraint [pk_student] primary key clustered
(
[id] asc
)with (pad_index = off, statistics_norecompute = off, ignore_dup_key = off, allow_row_locks = on, allow_page_locks = on) on [primary]
) on [primary]
go
插入数据
insert into test1.dbo.student(name,project,score)
values('张三','android','60'),
('张三','ios','70'),
('张三','html5','55'),
('张三','.net','100'),
('李四','android','60'),
('李四','ios','75'),
('李四','html5','90'),
('李四','.net','100');
使用Case When和聚合函数进行行专列
语法
select column_name,
<aggregation function>(<case when expression>)
from database.schema.table
group by column_name
语法解析
column_name
数据列列名
aggregation function
聚合函数,常见的有:sum,max,min,avg,count等。
case when expression
case when表达式
示例
select name,
max(case project when 'android' then score end) as '安卓',
max(case project when 'ios' then score end) as '苹果',
max(case project when 'html5' then score end) as 'html5',
max(case project when '.net' then score end) as '.net'
from [test1].[dbo].[student]
group by name
示例结果
转换前
转换后
使用PIVOT进行行专列
PIVOT通过将表达式中一列中的唯一值转换为输出中的多个列来旋转表值表达式。并PIVOT在最终输出中需要的任何剩余列值上运行聚合,PIVOT提供比一系列复杂的SELECT...CASE语句指定的语法更为简单和可读的语法,PIVOT执行聚合并将可能的多行合并到输出中的单个行中。
语法
select <non-pivoted column>,
[first pivoted column] as <column name>,
[second pivoted column] as <column name>,
...
[last pivoted column] as <column name>
from
(<select query that produces the data>)
as <alias for the source query>
pivot
(
<aggregation function>(<column being aggregated>)
for
[<column that contains the values that will become column headers>]
in ( [first pivoted column], [second pivoted column],
... [last pivoted column])
) as <alias for the pivot table>
<optional order by clause>;
语法解析
<non-pivoted column>
非聚合列。
[first pivoted column]
第一列列名。
[second pivoted column]
第二列列名。
[last pivoted column]
最后一列列名。
<select query that produces the data>
数据子表。
<alias for the source query>
表别名。
<aggregation function>
聚合函数。
<column being aggregated>
聚合函数列,用于输出值列,最终输出中返回的列(称为分组列)将对其进行分组。
[<column that contains the values that will become column headers>]
转换列,此列返回的唯一值将成为最终结果集中的字段。
[first pivoted column], [second pivoted column], ... [last pivoted column]
数据行中每一行行要转换的列名。
<optional order by clause>
排序规则。
示例
select b.Name,b.[android],b.[ios],b.[html5],b.[.net]
from
(select Name,Project,Score from [test1].[dbo].[student])
as a
pivot
(
max(Score)
for Project in ([android],[ios],[html5],[.net])
)
as b
order by b.name desc
示例结果
转换前
转换后
注意事项
1、如果输出列名不能在表转换列中,则不会执行任何计算。
2、输出的所有列的列名的数据类型必须一致。
来源:https://segmentfault.com/a/1190000019898109


猜你喜欢
- 我使用的Python3.5,32版本win764位系统,pandas0.19版本,使用df=pd.read_clipboard()的时候读不
- 有些时候我在们需要的用正则提取出html中某一个部分的文字内容,如图:获取dd部分的html文档,我们要通过它的一个属性去确定他的位置才可以
- 首先 编辑views.py文件每个响应对应一个函数 函数必须返回一个响应函数必须存在一个参数 一般约定为request每个响应函数 对应一个
- 分页一般和表格一起用,分页链接作为表格的一部分,将分页链接封装成一个独立的组件,然后作为子组件嵌入到表格组件中,这样比较合理。效果:代码:1
- 一、定义新的自动求导函数在底层,每个原始的自动求导运算实际上是两个在Tensor上运行的函数。其中,forward函数计算从输入Tensor
- 在使用数据库的时候,难免要在使用过程中进行删除的操作,如果是使用int类型的字段,令其自增长,这是个最简单的办法,但是后果会有些不是你想要的
- 一定要注重代码规范,按照平时的代码管理,可以将Python代码规范检测分为两种:静态本地检测:可以借助静态检查工具,比如:Flake8,Py
- 在日常生活中我们经常在朋友圈看到有人发九宫格图片,其实质就是将一张图片切成九份,然后在微信中一起发这九张图。那么我们如何自己动手实现呢?说到
- 创意404页面的文章我们似乎已经出过两篇了,今天hongkiat又带来了60个创意404页面.相关404页面设计文章:国外404错误页面的创
- python爬虫模块PyQuery简介PyQuery库也是一个非常强大又灵活的网页解析库,如果你有前端开发经验的,都应该接触过jQuery,
- 先来看一个例子:>>> def foo(*args, **kwargs): print
- js 数组对象操作方法如下:1. 创建数组var array1 = [1,2] //方法一var array2 = new Ar
- 函数的增益值torch.nn.init.calculate_gain(nonlinearity, param=None)提供了对非线性函数增
- 本文实例为大家分享了vue+moment实现倒计时的具体代码,供大家参考,具体内容如下示例代码<!-- 使用计算属性,传入截止日期 -
- 看到sam关于max-height的文章,觉得按捺不住了。sam注重于样式表的写法,过多的要求div+css的布局,sam可是追求艺术的人哦
- HP QR Code是一个PHP二维码生成类库,利用它可以轻松生成二维码,官网提供了下载和多个演示demo,查看地址:http://phpq
- 前言在部署一些定时运行或者长期运行的任务时,为了留存一些导致程序出现异常或错误的信息,通常会才用日志的方式来进行记录这些信息。python内
- Nodejs 的大部分核心 API 都是基于异步事件驱动设计的,事件驱动核心是通过 node 中 Events 对象来实现事件的发送和监听回
- 由于新云CMS系统,网站底部“版权信息”字段在数据库中是“文本”类型,有250个字符的限制。想在这里给加网站统计代码,因为字数限制的原因,就
- flask是我学习的第一个python的web框架,在应用flask写完一个应用后,当然是把它部署到我们的服务器上了。首先,准备我们部署项目