How do i transform rows into columns in sql server 2005(sql server 2005如何将行转换为列)
问题描述
这里有一个问题在具有相同标题的stackoverflow中,但这不是我想要的.
我有一张像下面这样的桌子
名称 |数数----------------奇瑞 |257德鲁 |1500摩根|13凯丝 |500柯克 |200马特 |76我需要把这个结果集转换成这样的东西
奇瑞 |德鲁 |摩根|凯丝 |柯克 |马特------------------------------------------257 1500 13 500 200 76如何使用 sql server 2005 实现这一点?
有类似问题这里,这里在stackoverflow中回答.
您需要使用运算符PIVOT 在您的查询中实现这一点.这是有关如何做到这一点的示例和说明.该示例引用自 这个来源.
---我假设你的表名是 TESTTABLE---声明@cols NVARCHAR(2000)声明 @query NVARCHAR(4000)SELECT @cols = STUFF(( SELECT DISTINCT TOP 100 PERCENT'],[' + t.Name从可测试的 tORDER BY '],[' + t.Name用于 XML 路径('')), 1, 2, '') + ']'SET @query = N'SELECT '+ @cols +' FROM(SELECT t1.Name , t1.Count FROM TESTTABLE AS t1) pPIVOT (MAX([Count]) FOR Name IN ('+ @cols +'))作为列兵;'执行(@查询)说明
1.查询的第一部分
SELECT @cols = STUFF(( SELECT DISTINCT TOP 100 PERCENT'],[' + t.Name从可测试的 tORDER BY '],[' + t.Name用于 XML 路径('')), 1, 2, '') + ']'在单行中为您的 Name 列值提供一个很好的扁平化结果,如下所示
[Cheryl],[Drew],[Karen],[Kath],[Kirk],[Matt]您可以了解更多关于 STUFF 和 XML PATH 这里 和 这里. p>
2.SELECT + @cols + FROM 将选择所有行作为最终结果集的列名(pvt - 步骤 3)
即
选择[Chery],[Drew],[Morgan],[Kath],[Kirk],[Matt]3.此查询提取我们创建交叉表结果所需的所有数据行.查询之后的 (p) 正在创建一个结果临时表,然后可以使用该表来满足第 1 步的查询.
(SELECT t1.Name, t1.Count FROM TESTTABLE AS t1) p4.PIVOT表达式
PIVOT (MAX (Count) FOR Name IN (@cols) AS pvt进行实际汇总并将结果放入名为 pvt as 的临时表中
奇瑞 |德鲁 |摩根|凯丝 |柯克 |马特------------------------------------------257 1500 13 500 200 76There is a question here in stackoverflow with the same title but that is not what I am looking for.
I have a table like the one below
Name | Count
----------------
Chery | 257
Drew | 1500
Morgon | 13
Kath | 500
Kirk | 200
Matt | 76
I need to trasform this result set into something like this
Chery | Drew | Morgon | Kath | Kirk | Matt
-------------------------------------------
257 1500 13 500 200 76
How do i acheive this using sql server 2005?
There are similar questions here,here answered in stackoverflow.
You need to use the operator PIVOT in your query to acheive this.Here is the example and explanation on how you can do that.The example is referenced from this source.
---I assumed your tablename as TESTTABLE---
DECLARE @cols NVARCHAR(2000)
DECLARE @query NVARCHAR(4000)
SELECT @cols = STUFF(( SELECT DISTINCT TOP 100 PERCENT
'],[' + t.Name
FROM TESTTABLE AS t
ORDER BY '],[' + t.Name
FOR XML PATH('')
), 1, 2, '') + ']'
SET @query = N'SELECT '+ @cols +' FROM
(SELECT t1.Name , t1.Count FROM TESTTABLE AS t1) p
PIVOT (MAX([Count]) FOR Name IN ( '+ @cols +' ))
AS pvt;'
EXECUTE(@query)
Explanation
1.The first part of the query
SELECT @cols = STUFF(( SELECT DISTINCT TOP 100 PERCENT
'],[' + t.Name
FROM TESTTABLE AS t
ORDER BY '],[' + t.Name
FOR XML PATH('')
), 1, 2, '') + ']'
gives you a nice flattened result of your Name column values in a single row as follow
[Cheryl],[Drew],[Karen],[Kath],[Kirk],[Matt]
You can learn more about the STUFF and XML PATH here and here.
2.SELECT + @cols + FROM will select all the rows as coloumn names for the final result set (pvt - step 3)
i.e
Select [Chery],[Drew],[Morgan],[Kath],[Kirk],[Matt]
3.This query pulls all the rows of data that we need to create the cross-tab results. The (p) after the query is creating a temporary table of the results that can then be used to satisfy the query for step 1.
(SELECT t1.Name, t1.Count FROM TESTTABLE AS t1) p
4.The PIVOT expression
PIVOT (MAX (Count) FOR Name IN ( @cols) AS pvt
does the actual summarization and puts the results into a temporary table called pvt as
Chery | Drew | Morgon | Kath | Kirk | Matt
-------------------------------------------
257 1500 13 500 200 76
这篇关于sql server 2005如何将行转换为列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:sql server 2005如何将行转换为列
基础教程推荐
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
- 无法解决整理冲突 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- 是否可以执行按位分组功能? 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
