iis服务器助手广告广告
返回顶部
首页 > 资讯 > 数据库 >SQL Server中怎么将行数据转换为列数据
  • 348
分享到

SQL Server中怎么将行数据转换为列数据

2024-04-02 19:04:59 348人浏览 薄情痞子
摘要

今天就跟大家聊聊有关SQL Server中怎么将行数据转换为列数据,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。准备工作创建表use [t

今天就跟大家聊聊有关SQL Server中怎么将行数据转换为列数据,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

准备工作

创建表

use [test1]Gocreate 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.tablegroup 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 apivot(  max(Score)  for Project in ([android],[ios],[html5],[.net])) as border by b.name desc

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

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

看完上述内容,你们对sql Server中怎么将行数据转换为列数据有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注编程网数据库频道,感谢大家的支持。

您可能感兴趣的文档:

--结束END--

本文标题: SQL Server中怎么将行数据转换为列数据

本文链接: https://www.lsjlt.com/news/58038.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • sql怎么查看表的索引
    通过查询系统表,可以获取表的索引信息,包括索引名称、是否唯一、索引类型、索引列和行数。常用系统表有:mysql 的 information_schema.statistics、postg...
    99+
    2024-05-14
    mysql oracle
  • sql怎么查看索引
    您可以使用 sql 通过以下方法查看索引:show indexes 语句:显示表中定义的索引列表及其信息。explain 语句:显示查询计划,其中包含用于执行查询的索引。informat...
    99+
    2024-05-14
  • sql怎么查看存储过程
    如何查看 sql 存储过程的源代码:使用 show create procedure 语句直接获取创建脚本。查询 information_schema.routines 表的 routi...
    99+
    2024-05-14
  • sql怎么查看视图表
    要查看视图表,可以使用以下步骤:使用 select 语句获取视图中的数据。使用 desc 语句查看视图的架构。使用 explain 语句分析视图的执行计划。使用 dbms 提供...
    99+
    2024-05-14
    oracle python
  • sql怎么查看创建的视图
    可以通过sql查询查看已创建的视图,具体步骤包括:连接到数据库并执行查询select * from information_schema.views;查询结果将显示视图的名称、...
    99+
    2024-05-14
    mysql
  • sql怎么用循环语句实现查询
    可以通过 do 和 while 语句创建循环,并在循环内执行查询,详细步骤包括:定义循环变量设置循环初始值循环执行查询更新循环变量执行查询循环退出条件 SQL 中使用循环语句实现查询 ...
    99+
    2024-05-14
  • sql怎么用代码修改表中数据
    通过 sql 代码修改表中数据的方法包括:修改单个记录:使用 update 语句设置列值并指定条件。修改多条记录:在 update 语句中指定多个条件来修改满足条件的所有记录。增加新列:...
    99+
    2024-05-14
  • sql怎么用命令创建数据库
    在 sql 中使用 create database 命令创建新数据库,其语法包含以下步骤:指定数据库名称。指定数据库文件和日志文件的位置(可选)。指定数据库大小、最大大小和文件增长(可选...
    99+
    2024-05-14
  • sql怎么用身份证提取年龄
    sql 中提取身份证号码中的年龄的方法:提取出生日期部分(身份证号码中第 7-14 位);使用 to_date 函数转换为日期格式;使用 extract 函数计算与当前日期之间的年差。 ...
    99+
    2024-05-14
  • sql怎么看字段长度
    有两种方法可查看 sql 中的字段长度:使用 information_schema 架构,其中包含元数据信息,可用于查询字段长度。使用内建函数,如 length(),其适用于字符串数据类...
    99+
    2024-05-14
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作