南强小屋 Design By 杰米

准备工作

创建表

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

示例结果

转换前

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

转换后

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

使用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

示例结果

转换前

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

转换后

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

注意事项

1、如果输出列名不能在表转换列中,则不会执行任何计算。

2、输出的所有列的列名的数据类型必须一致。

总结

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

标签:
sqlserver行转列,sqlserver列转行

南强小屋 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
南强小屋 Design By 杰米

评论“SQL Server基础之行数据转换为列数据”

暂无SQL Server基础之行数据转换为列数据的评论...

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。