简体   繁体   English

Pivot表格成多列多行

[英]Pivot table into multiple columns and multiple rows

Background I am have a table which contains a varchar with a comma separated string (which has multiple rows similar to this question. Unfortunately I haven't been able to find a way to change a csv varchar into a temp table simply so I am looking into splitting it up myself. (I would also appreciate any answers tackling this, although maybe it should be a separate question?).背景我有一个表,其中包含一个带有逗号分隔字符串的 varchar(它有多个类似于这个问题的行。不幸的是,我无法找到将 csv varchar 更改为临时表的方法,所以我正在寻找自己拆分它。(我也很感激任何解决这个问题的答案,尽管它可能应该是一个单独的问题?)。

Question问题

I have successfully split the string up and am now producing an output similar to:我已成功拆分字符串,现在正在生成类似于以下内容的 output:

   -------------------------------------
   | Row Index  | Column Index | Value |
   -------------------------------------
   |     0      |      0       | (0,0) |
   |     0      |      1       | (1,0) |
   |     0      |      2       | (2,0) |
   |     1      |      0       | (0,1) |
   |     1      |      1       | (1,1) |
   |     1      |      2       | (2,1) |
   |     2      |      0       | (0,2) |
   |     2      |      1       | (1,2) |
   |     2      |      2       | (2,2) |
   -------------------------------------

I would like to pivot this so that I can insert it into a temp table and the final result would look like:我想 pivot 这样我可以将它插入临时表,最终结果如下所示:

   -------------------------------------
   | Column 1  | Column 2  | Column 3  |
   -------------------------------------
   |   (0,0)   |   (1,0)   |   (2,0)   |
   |   (0,1)   |   (1,1)   |   (2,1)   |
   |   (0,2)   |   (1,2)   |   (2,2)   |
   -------------------------------------

Asides旁白

  1. The number of columns is known ahead of time but answers which don't depend on this are also welcome.列数是提前知道的,但也欢迎不依赖于此的答案。
  2. I know that I could repeatedly left outer join the first table to get this result but this requires outer joining once for every column (as I have ~9 columns this will be a lot of repetition) and I assume that there is another way.我知道我可以反复左外连接第一个表来获得这个结果,但这需要每列外连接一次(因为我有大约 9 列,这将是很多重复),我假设还有另一种方法。
  3. Performance is not key but there will be ~2000 rows with 8 columns in the final table.性能不是关键,但最终表中将有约 2000 行和 8 列。

using conditional aggregation: 使用条件聚合:

select 
    RowIndex
  , Column1 = max(case when ColumnIndex=0 then Value end)
  , Column2 = max(case when ColumnIndex=1 then Value end)
  , Column3 = max(case when ColumnIndex=2 then Value end)
from t
group by RowIndex

rextester demo: http://rextester.com/QLPHR39222 extrester演示: http ://rextester.com/QLPHR39222

returns: 返回:

+----------+---------+---------+---------+
| RowIndex | Column1 | Column2 | Column3 |
+----------+---------+---------+---------+
|        0 | (0,0)   | (1,0)   | (2,0)   |
|        1 | (0,1)   | (1,1)   | (2,1)   |
|        2 | (0,2)   | (1,2)   | (2,2)   |
+----------+---------+---------+---------+

or with pivot() : 或使用pivot()

select 
    RowIndex
  , Column1 = [0]
  , Column2 = [1]
  , Column3 = [2]
from t
pivot (max(Value) for ColumnIndex in ([0],[1],[2])) p

This is a straightforward application of a PIVOT query ( See The Docs ): 这是PIVOT查询的直接应用程序( 请参阅The Docs ):

select x.[0] Column1
     , x.[1] Column2
     , x.[2] Column3
  from YourData
 pivot (max(Value)
   for [Column Index]
    in ([0], [1], [2]) ) x
 order by x.[Row Index]

which Returns: 返回:

| Column1 | Column2 | Column3 |
|---------|---------|---------|
|   (0,0) |   (1,0) |   (2,0) |
|   (0,1) |   (1,1) |   (2,1) |
|   (0,2) |   (1,2) |   (2,2) |

SQL Fiddle SQL小提琴

To add more columns simply add more column indexes to the FOR column IN (list) section, and add the same values to the projection (select list) 要添加更多列,只需将更多列索引添加到FOR column IN (list)部分,然后将相同的值添加到投影(选择列表)

I have a very similar question to this but the only difference is I have more than 3 in Rox index basically have 4 million and column index has more than 0,1 2 which is a rep_name field and can have more than 100 and keeps changing as new reps add or remove.我有一个与此非常相似的问题,但唯一的区别是我在 Rox 索引中有超过 3 个,基本上有 400 万个,列索引有超过 0,1 2 个,这是一个 rep_name 字段,可以有超过 100 个并且不断变化为新代表添加或删除。 Value field should be filtered to have only 3 types of teams and should be pivoted and merged to have ex: 'X_team & Y team' by each row index值字段应过滤为仅具有 3 种类型的团队,并且应按每个行索引进行旋转和合并以具有 ex: 'X_team & Y team'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM