1

我不确定如何将我的数据转至特定视图。下面是测试数据。

SQL

CREATE TABLE #tmpData (ProductTitle VARCHAR(100), abvrMonthName VARCHAR(3),abvrMonthNameCount VARCHAR(4),MonthAvg NUMERIC(6,2),MonthCount INT)
INSERT INTO #tmpData SELECT 'Product 1','Dec','Dec#', 0 , 0
INSERT INTO #tmpData SELECT 'Product 1','Nov','Nov#', 0 , 0
INSERT INTO #tmpData SELECT 'Product 1','Oct','Oct#', 0 , 0
INSERT INTO #tmpData SELECT 'Product 1','Sep','Sep#', 0 , 0
INSERT INTO #tmpData SELECT 'Product 1','Aug','Aug#', 0 , 0
INSERT INTO #tmpData SELECT 'Product 1','Jul','Jul#', 0 , 0
INSERT INTO #tmpData SELECT 'Product 1','Jun','Jun#', 5 , 1
INSERT INTO #tmpData SELECT 'Product 1','May','May#', 4.44 , 9
插入 #tmpData SELECT 'Product 1','Apr','Apr#', 5 , 6
INSERT INTO #tmpData SELECT 'Product 1','Mar','Mar#', 5 , 4
INSERT INTO #tmpData SELECT 'Product 1','Feb','Feb#', 5 , 1
INSERT INTO #tmpData SELECT 'Product 1','Jan','Jan#', 5 , 2
INSERT INTO #tmpData SELECT 'Product 2','Dec','Dec#', 0 , 0
INSERT INTO #tmpData SELECT 'Product 2','Nov','Nov#', 0 , 0
INSERT INTO #tmpData SELECT 'Product 2','Oct','Oct#', 0 , 0
INSERT INTO #tmpData SELECT 'Product 2','Sep','Sep#', 0 , 0
INSERT INTO #tmpData SELECT 'Product 2','Aug','Aug#', 0 , 0
INSERT INTO #tmpData SELECT 'Product 2','Jul','Jul#', 0 , 0
INSERT INTO #tmpData SELECT 'Product 2','Jun','Jun#', 5 , 1
INSERT INTO #tmpData SELECT 'Product 2','May','May#', 4.67 , 3
INSERT INTO #tmpData SELECT 'Product 2','Apr','Apr#', 4.33 , 3
INSERT INTO #tmpData SELECT 'Product 2','Mar','Mar#', 0 , 0
INSERT INTO #tmpData SELECT 'Product 2','Feb','Feb#', 0 , 0
INSERT INTO #tmpData SELECT 'Product 2','Jan','Jan#', 0 , 0
INSERT INTO #tmpData SELECT 'Product 3','Dec','Dec#', 0 , 0
INSERT INTO #tmpData SELECT 'Product 3','Nov','Nov#', 0 , 0
INSERT INTO #tmpData SELECT 'Product 3','Oct','Oct#', 0 , 0
INSERT INTO #tmpData SELECT 'Product 3','Sep','Sep#', 0 , 0
INSERT INTO #tmpData SELECT 'Product 3','Aug','Aug#', 0 , 0
INSERT INTO #tmpData SELECT 'Product 3','Jul','Jul#', 0 , 0
INSERT INTO #tmpData SELECT 'Product 3','Jun','Jun#', 5 , 3
INSERT INTO #tmpData SELECT 'Product 3','May','May#', 5 , 6
INSERT INTO #tmpData SELECT 'Product 3','Apr','Apr#', 4 , 6
INSERT INTO #tmpData SELECT 'Product 3','Mar','Mar#', 4.75 , 8
INSERT INTO #tmpData SELECT 'Product 3','Feb','Feb#', 4.75 , 8
INSERT INTO #tmpData SELECT 'Product 3','Jan','Jan#', 4.6 , 5

选择产品标题,[jan],[jan#],[feb],[feb#]
    从    
    ( 选择 *
        来自#tmpData        
    ) 作为小号
    枢
    (
        SUM(MonthAvg) FOR abvrMonthName IN (
            一月、二月、三月、四月、五月、六月、七月、八月、九月、十月、十一月、[十二月]
        )
    ) 作为 p  
    枢
    (
        SUM(MonthCount) FOR abvrMonthNameCount IN (
            jan#,feb#,mar#,apr#,may#,jun#,jul#,aug#, sep#, oct#, nov#, [dec#]
        )
    ) 作为 p1
    --按产品标题分组,[一月],[二月]

删除表#tmpData

从输出中可以看出,ProductTitle没有分组。

我将如何实现这一目标,还是我完全走错了方向?

4

1 回答 1

2

部分问题是您在要旋转的多个列中具有非规范化数据。理想情况下,您应该考虑修复表结构,以便更容易维护和查询。如果您无法修复表结构,那么您应该先对列进行反透视,然后再应用 PIVOT 以获得最终结果。

UNPIVOT 进程将获取多列并将它们转换为多行。根据您的 SQL Server 版本,有几种方法可以执行此操作。您可以使用 UNPIVOT 函数,或者由于您使用的是 SQL Server 2008,因此您可以使用带有 VALUES 子句的 CROSS APPLY 来取消透视。

CROSS APPLY/VALUES 代码将是:

select t.producttitle, c.col, c.value
from tmpData t
cross apply
(
  values (abvrMonthName, MonthAvg), (abvrMonthNameCount, MonthCount)
) c (col, value)

请参阅SQL Fiddle with Demo。这需要您的多个列并将数据放入类似于以下的格式:

| PRODUCTTITLE |  COL | VALUE |
-------------------------------
|    Product 1 |  Dec |     0 |
|    Product 1 | Dec# |     0 |
|    Product 1 |  Nov |     0 |
|    Product 1 | Nov# |     0 |
|    Product 1 |  Oct |     0 |
|    Product 1 | Oct# |     0 |
|    Product 1 |  Sep |     0 |
|    Product 1 | Sep# |     0 |

一旦数据采用这种格式,您就可以将 PIVOT 应用于col包含月份名称的值:

select producttitle, jan, [jan#], feb, [feb#], mar, [mar#], apr, [apr#],
  may, [may#], jun, [jun#], jul, [jul#], aug, [aug#],
  sep, [sep#], oct, [oct#], nov, [nov#], dec, [dec#]
from
(
  select t.producttitle, c.col, c.value
  from tmpData t
  cross apply
  (
    values (abvrMonthName, MonthAvg), (abvrMonthNameCount, MonthCount)
  ) c (col, value)
) d
pivot
(
  sum(value)
  for col in (jan, [jan#], feb, [feb#], mar, [mar#], apr, [apr#],
              may, [may#], jun, [jun#], jul, [jul#], aug, [aug#],
              sep, [sep#], oct, [oct#], nov, [nov#], dec, [dec#])
) piv;

请参阅SQL Fiddle with Demo。这给出了一个结果:

| PRODUCTTITLE | JAN | JAN# |  FEB | FEB# |  MAR | MAR# |  APR | APR# |  MAY | MAY# | JUN | JUN# | JUL | JUL# | AUG | AUG# | SEP | SEP# | OCT | OCT# | NOV | NOV# | DEC | DEC# |
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|    Product 1 |   5 |    2 |    5 |    1 |    5 |    4 |    5 |    6 | 4.44 |    9 |   5 |    1 |   0 |    0 |   0 |    0 |   0 |    0 |   0 |    0 |   0 |    0 |   0 |    0 |
|    Product 2 |   0 |    0 |    0 |    0 |    0 |    0 | 4.33 |    3 | 4.67 |    3 |   5 |    1 |   0 |    0 |   0 |    0 |   0 |    0 |   0 |    0 |   0 |    0 |   0 |    0 |
|    Product 3 | 4.6 |    5 | 4.75 |    8 | 4.75 |    8 |    4 |    6 |    5 |    6 |   5 |    3 |   0 |    0 |   0 |    0 |   0 |    0 |   0 |    0 |   0 |    0 |   0 |    0 |
于 2013-06-04T12:15:10.080 回答