设为首页收藏本站

EPS数据狗论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1642|回复: 0

SQLServer之行数据转换为列数据

[复制链接]

14

主题

293

金钱

373

积分

入门用户

发表于 2019-8-2 15:43:17 | 显示全部楼层 |阅读模式

准备工作
创建表
  1. use [test1]
  2. go

  3. create table [dbo].[student](
  4.     [id] [int] identity(1,1) not null,
  5.     [name] [nvarchar](50) null,
  6.     [project] [nvarchar](50) null,
  7.     [score] [int] null,
  8. constraint [pk_student] primary key clustered
  9. (
  10.     [id] asc
  11. )with (pad_index = off, statistics_norecompute = off, ignore_dup_key = off, allow_row_locks = on, allow_page_locks = on) on [primary]
  12. ) on [primary]
  13. go
复制代码


插入数据
  1. insert into test1.dbo.student(name,project,score)
  2. values('张三','android','60'),
  3.       ('张三','ios','70'),
  4.       ('张三','html5','55'),
  5.       ('张三','.net','100'),
  6.       ('李四','android','60'),
  7.       ('李四','ios','75'),
  8.       ('李四','html5','90'),
  9.       ('李四','.net','100');
复制代码


使用Case When和聚合函数进行行专列
语法
  1. select column_name,
  2. <aggregation function>(<case when expression>)  
  3. from database.schema.table
  4. group by column_name
复制代码


语法解析
column_name

数据列列名

aggregation function

聚合函数,常见的有:sum,max,min,avg,count等。

case when expression

case when表达式

示例
  1. select name,
  2. max(case project when 'android' then score end) as '安卓',
  3. max(case project when 'ios' then score end) as '苹果',
  4. max(case project when 'html5' then score end) as 'html5',
  5. max(case project when '.net' then score end) as '.net'
  6. from [test1].[dbo].[student]
  7. group by name
复制代码


示例结果
转换前
1.png
转换后
2.png

使用PIVOT进行行专列
PIVOT通过将表达式中一列中的唯一值转换为输出中的多个列来旋转表值表达式。并PIVOT在最终输出中需要的任何剩余列值上运行聚合,PIVOT提供比一系列复杂的SELECT...CASE语句指定的语法更为简单和可读的语法,PIVOT执行聚合并将可能的多行合并到输出中的单个行中。

语法
  1. select <non-pivoted column>,  
  2.     [first pivoted column] as <column name>,  
  3.     [second pivoted column] as <column name>,  
  4.     ...  
  5.     [last pivoted column] as <column name>  
  6. from  
  7.     (<select query that produces the data>)   
  8.     as <alias for the source query>  
  9. pivot  
  10. (  
  11.     <aggregation function>(<column being aggregated>)  
  12. for   
  13. [<column that contains the values that will become column headers>]   
  14.     in ( [first pivoted column], [second pivoted column],  
  15.     ... [last pivoted column])  
  16. ) as <alias for the pivot table>  
  17. <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>

排序规则。


示例
  1. select b.Name,b.[android],b.[ios],b.[html5],b.[.net]
  2. from  
  3. (select Name,Project,Score from [test1].[dbo].[student])
  4. as a
  5. pivot
  6. (
  7.     max(Score)
  8.     for Project in ([android],[ios],[html5],[.net])
  9. )
  10. as b
  11. order by b.name desc
复制代码


示例结果
转换前
3.png
转换后
4.png
注意事项
1、如果输出列名不能在表转换列中,则不会执行任何计算。
2、输出的所有列的列名的数据类型必须一致。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐上一条 /1 下一条

客服中心
关闭
在线时间:
周一~周五
8:30-17:30
QQ群:
653541906
联系电话:
010-85786021-8017
在线咨询
客服中心

意见反馈|网站地图|手机版|小黑屋|EPS数据狗论坛 ( 京ICP备09019565号-3 )   

Powered by BFIT! X3.4

© 2008-2028 BFIT Inc.

快速回复 返回顶部 返回列表