Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
686 views
in Technique[技术] by (71.8m points)

sql - 在SQL Server中使用“数据透视表”将行转换为列(Convert Rows to columns using 'Pivot' in SQL Server)

I have read the stuff on MS pivot tables and I am still having problems getting this correct.

(我已经阅读了MS数据透视表上的内容,但在获得正确答案方面仍然遇到问题。)

I have a temp table that is being created, we will say that column 1 is a Store number, and column 2 is a week number and lastly column 3 is a total of some type.

(我有一个正在创建的临时表,我们会说第1列是商店号,第2列是星期数,最后第3列是某种类型的总数。)

Also the Week numbers are dynamic, the store numbers are static.

(同样,周号是动态的,商店号是静态的。)

Store      Week     xCount
-------    ----     ------
102        1        96
101        1        138
105        1        37
109        1        59
101        2        282
102        2        212
105        2        78
109        2        97
105        3        60
102        3        123
101        3        220
109        3        87

I would like it to come out as a pivot table, like this:

(我希望将其作为数据透视表发布,如下所示:)

Store        1          2          3        4        5        6....
----- 
101        138        282        220
102         96        212        123
105         37        
109

Store numbers down the side and weeks across the top.

(将数字存储在侧面,数周存储在顶部。)

  ask by Lynn translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you are using SQL Server 2005+, then you can use the PIVOT function to transform the data from rows into columns.

(如果使用的是SQL Server 2005+,则可以使用PIVOT函数将数据从行转换为列。)

It sounds like you will need to use dynamic sql if the weeks are unknown but it is easier to see the correct code using a hard-coded version initially.

(听起来好像如果周数未知,您将需要使用动态sql,但是最初使用硬编码版本更容易查看正确的代码。)

First up, here are some quick table definitions and data for use:

(首先,这里是一些快速的表定义和要使用的数据:)

CREATE TABLE #yt 
(
  [Store] int, 
  [Week] int, 
  [xCount] int
);

INSERT INTO #yt
(
  [Store], 
  [Week], [xCount]
)
VALUES
    (102, 1, 96),
    (101, 1, 138),
    (105, 1, 37),
    (109, 1, 59),
    (101, 2, 282),
    (102, 2, 212),
    (105, 2, 78),
    (109, 2, 97),
    (105, 3, 60),
    (102, 3, 123),
    (101, 3, 220),
    (109, 3, 87);

If your values are known, then you will hard-code the query:

(如果您的值已知,则将对查询进行硬编码:)

select *
from 
(
  select store, week, xCount
  from yt
) src
pivot
(
  sum(xcount)
  for week in ([1], [2], [3])
) piv;

See SQL Demo

(请参阅SQL演示)

Then if you need to generate the week number dynamically, your code will be:

(然后,如果您需要动态生成星期数,则代码将是:)

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

select @cols = STUFF((SELECT ',' + QUOTENAME(Week) 
                    from yt
                    group by Week
                    order by Week
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = 'SELECT store,' + @cols + ' from 
             (
                select store, week, xCount
                from yt
            ) x
            pivot 
            (
                sum(xCount)
                for week in (' + @cols + ')
            ) p '

execute(@query);

See SQL Demo .

(请参见SQL Demo 。)

The dynamic version, generates the list of week numbers that should be converted to columns.

(动态版本生成应转换为列的week数列表。)

Both give the same result:

(两者给出相同的结果:)

| STORE |   1 |   2 |   3 |
---------------------------
|   101 | 138 | 282 | 220 |
|   102 |  96 | 212 | 123 |
|   105 |  37 |  78 |  60 |
|   109 |  59 |  97 |  87 |

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...