1

我的数据看起来像

Viral Impressions   Paid Impressions    Organic Impressions
123                   5678                          34566

我希望它采用这种格式。

         Impressions
Viral        123
Paid         5678
Organic       34566

我如何在 SSIS 中使用 Pivot unpivot 做到这一点

4

2 回答 2

1

作为 UNPIVOT 的替代方法,您可以使用 UNION ALL:

 Select 'viral' as col, [viral impressions] as impressions
 From yourtable
 Union all
 Select 'paid' as col, [paid impressions] as impressions
 From yourtable
 Union all
 Select 'organic' as col, [organic impressions] as impressions
 From yourtable

请参阅带有演示的 SQL Fiddle

给出结果:

|     COL | IMPRESSIONS |
-------------------------
|   viral |         123 |
|    paid |        5678 |
| organic |       34566 |
于 2013-01-08T16:00:01.553 回答
0

使用UNPIVOT表运算符:

SELECT 
  value, 
  Impressions
FROM TableName
UNPIVOT
(
  Impressions
  FOR value IN([Viral], 
               [Paids], 
               [Organic])
) u;

SQL 小提琴演示

这假设列的名称存储为Viral | Paid | Organic. 如果这些列的名称是Viral Impressions | Paid Impressions | Organic Impressions那么你可以这样做:

WITH CTE
AS
(
  SELECT value, Impressions
  FROM Table1
  UNPIVOT
  (
     Impressions
     FOR value IN([Viral Impressions], 
                  [Paid Impressions], 
                  [Organic Impressions])
  ) u
)
SELECT 
  REPLACE(value, ' Impressions', '') Value, 
  Impressions
FROM CTE;

更新的 SQL Fiddle 演示

于 2013-01-08T15:12:58.723 回答