SQL Server基础之行数据转换为列数据

所属分类: 数据库 / MsSql 阅读数: 1722
收藏 0 赞 0 分享

准备工作

创建表

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、输出的所有列的列名的数据类型必须一致。

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。

更多精彩内容其他人还在看

对有insert触发器表取IDENTITY值时发现的问题

赶快查了下msdn,原来@@IDENTITY还有这么多讲究
收藏 0 赞 0 分享

SQL SERVER 查询正在实行的SQL语句

SQL SERVER 查询正在实行的SQL语句的实现代码
收藏 0 赞 0 分享

sql 随机抽取几条数据的方法 推荐

前段时间在做项目的时刻。总是遇到这样一个问题。就是要怎么去让首页显示的内容不断的变化。想了很久。也没有什么结果。后面去想了一下。得出以下一个结果
收藏 0 赞 0 分享

sql 多条件组合查询,并根据指定类别找出所有最小子类别的SQL语句备忘

多条件组合查询,并根据指定类别找出所有最小子类别的SQL语句备忘
收藏 0 赞 0 分享

Java 实现连接sql server 2000

JDBC技术事实上是一种能通过JAVA语言访问任何结构化数据库的应用程序接口(API)(Sun这样说的,我也不知道是不是真的),而且现在的JDBC 3.0据Sun说也能访问Execel等电子表格程序!
收藏 0 赞 0 分享

SQL Server 不存在或访问被拒绝(转)

在使用 SQL Server 的过程中,用户遇到最多的问题莫过于连接失败了。一般而言,有两种连接SQL Server 的方式,一是利用 SQL Server 自带的客户端工具
收藏 0 赞 0 分享

分页查询 效率最高

给大家分享个效率最高的分页查询 5000万级别有效 比 ROWNUMBER 和Top效率高
收藏 0 赞 0 分享

sqlserver 系统存储过程 中文说明

sqlserver 系统存储过程这样大家就知道这些存储过程的作用了。
收藏 0 赞 0 分享

sql 多表连接查询

sql 多表连接查询语句代码,大家可以参考下。
收藏 0 赞 0 分享

SQL SERVER 自增列

判断Table是否存在自增列(Identity column)
收藏 0 赞 0 分享
查看更多