如何在SQL Server 2005中将行转换为列 [英] How do i transform rows into columns in sql server 2005

查看:81
本文介绍了如何在SQL Server 2005中将行转换为列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此处有一个问题在具有相同标题的stackoverflow中,但这不是我想要的.
我有一张下面的桌子

Name   | Count  
----------------    
Chery  | 257  
Drew   | 1500
Morgon | 13  
Kath   | 500  
Kirk   | 200  
Matt   | 76 

我需要将此结果集转换为类似

Chery | Drew | Morgon | Kath | Kirk | Matt  
-------------------------------------------  
257     1500     13      500    200    76

我如何使用sql server 2005做到这一点?

解决方案

也有类似的问题此处以stackoverflow的方式回答.

您需要使用运算符 PIVOT 来实现此目的.以下是示例和有关如何执行此操作的说明.该示例可从

[Cheryl],[Drew],[Karen],[Kath],[Kirk],[Matt]  

您可以在此处此处.

2. SELECT + @cols + FROM将选择所有行作为最终结果集的列名(pvt-步骤3)

Select [Chery],[Drew],[Morgan],[Kath],[Kirk],[Matt] 

3.此查询提取创建交叉表结果所需的所有数据行.查询后的(p)将创建一个临时结果表,然后该表可用于满足步骤1的查询.

(SELECT t1.Name, t1.Count FROM  TESTTABLE AS t1) p

4.PIVOT表达式

PIVOT (MAX (Count) FOR Name IN ( @cols) AS pvt

进行实际的汇总,并将结果放入称为pvt的临时表中,作为

Chery | Drew | Morgon | Kath | Kirk | Matt  
-------------------------------------------  
257     1500     13      500    200    76

There 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中将行转换为列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆