SQL Server 2008 R2 使用 PIVOT 和 varchar 列不起作用

SQL Server 2008 R2 using PIVOT with varchar columns not working(SQL Server 2008 R2 使用 PIVOT 和 varchar 列不起作用)

本文介绍了SQL Server 2008 R2 使用 PIVOT 和 varchar 列不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 SQL Server 2008 R2,我有这个简单的表

I'm using SQL Server 2008 R2, I have this simple table

我试图做的是从这个表中进行选择并得到以下结果

What I was trying to do is make a selection from this table and get this following result

x |      1     |       2     |      3
--+------------+-------------+------------
1 |   first 1  |    first 2  |    first 3
2 |   Second 1 |    second 2 |    second 3 

我认为可以使用 PIVOT

我不太了解 PIVOT 以及使用 PIVOT 和 Count() 找到的所有搜索结果.SUM(), AVG() 这在我的表中不起作用,因为我试图在 varcharPIVOT代码>列

I don't know much about PIVOT AND all my search result found using PIVOT with Count() . SUM(), AVG() which will not work in my table since I'm trying to PIVOT on a varchar column

问题我是否使用了正确的功能?或者我还需要知道什么来解决这个问题?任何帮助将不胜感激

Question am I using the right function? Or is there something else I need to know to solve this issue? Any help will be appreciated

我试过这个没有运气

PIVOT(count(x) FOR value IN ([1],[2],[3]) )as total 
PIVOT(count(y) FOR value IN ([1],[2],[3]) )as total  // This one is the nearest 
of what i wand  but instead of the column value values i get 0  

这里是查询是否有人测试它

Here is the query if any one to test it

CREATE TABLE #test (x int , y int , value Varchar(50))
INSERT INTO #test VALUES(1,51,'first 1')
INSERT INTO #test VALUES(1,52,'first 2')
INSERT INTO #test VALUES(1,53,'first 3')
INSERT INTO #test VALUES(2,51,'Second 1')
INSERT INTO #test VALUES(2,52,'Second 2')
INSERT INTO #test VALUES(2,53,'Second 3')
SELECT * FROM #test
  PIVOT(count(y) FOR value IN ([1],[2],[3]) )as total 
 DROP TABLE #test 

推荐答案

当您使用 PIVOT 函数时,IN 子句中的值需要与您选择的值匹配.您当前的数据不包括 1、2 或 3.您可以使用 row_number() 为每个 x 分配一个值:

When you are using the PIVOT function the values inside the IN clause need to match a value that you are selecting. Your current data does not include 1, 2, or 3. You can use row_number() to assign a value for each x:

select x, [1], [2], [3]
from
(
  select x, value,
    row_number() over(partition by x order by y) rn
  from test
) d
pivot
(
  max(value)
  for rn in ([1], [2], [3])
) piv;

参见 SQL Fiddle with Demo.如果您对每个 x 有未知数量的值,那么您将需要使用动态 SQL:

See SQL Fiddle with Demo. If you then have a unknown number of values for each x, then you will want to use dynamic SQL:

DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)

select @cols = STUFF((SELECT distinct ',' + QUOTENAME(row_number() over(partition by x order by y)) 
                    from test
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = 'SELECT x,' + @cols + ' 
            from 
            (
              select x, value,
                row_number() over(partition by x order by y) rn
              from test
            ) x
            pivot 
            (
                max(value)
                for rn in (' + @cols + ')
            ) p '

execute(@query);

参见 SQL Fiddle with Demo

这篇关于SQL Server 2008 R2 使用 PIVOT 和 varchar 列不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:SQL Server 2008 R2 使用 PIVOT 和 varchar 列不起作用

基础教程推荐