PIVOT有多列

时间:2014-12-12 22:25:57

标签: sql-server sql-server-2008 pivot unpivot

我正在尝试在发票表上的SQL Server 2008中的两列上进行数据透视。所以我有如下数据:

+--------------+--------+---------+------+
| Invoice Date | Item # | Dollars | Lbs. |
+--------------+--------+---------+------+
| 1/1/14       | A      |       1 |    1 |
| 1/2/14       | B      |       2 |    2 |
| 1/3/14       | A      |       3 |    3 |
| 1/4/14       | B      |       4 |    4 |
| 2/1/14       | A      |       5 |    5 |
| 2/1/14       | B      |       6 |    6 |
+--------------+--------+---------+------+

我想将其显示为

+--------+--------------+-----------------+--------------+-----------------+
| Item # | 1/31/14 Lbs. | 1/31/14 Dollars | 2/28/14 Lbs. | 2/28/14 Dollars |
+--------+--------------+-----------------+--------------+-----------------+
| A      |            4 |               4 |            5 |               5 |
| B      |            6 |               6 |            6 |               6 |
+--------+--------------+-----------------+--------------+-----------------+

请注意,列名称是该月的最后一天,以美元或磅为单位。我可以做得很好一列(无论是英镑还是美元),但我不能两者都做。

这是我的仅仅磅的示例代码:

DECLARE
  @v_Columns VARCHAR(MAX),
  @v_Query VARCHAR(MAX)

  --pivot and delimit values

SELECT @v_Columns = COALESCE(@v_Columns,'[') + convert(varchar(8), InvoiceDate, 1) + ' Lbs.' + '],[' 
FROM 
( SELECT DISTINCT dbo.ufn_GetLastDayOfMonth(InvoiceDate) As InvoiceDate
  FROM Invoice 
  WHERE InvoiceDate BETWEEN @BEGIN_DATE AND @END_DATE
  ORDER BY InvoiceDate

--delete last two chars of string (the ending ',[')

SET @v_Columns = SUBSTRING(@v_Columns, 1, LEN(@v_Columns)-2)
PRINT @v_Columns

--construct sql statement

SET @v_Query = 

'WITH AllOrders (LastInvoiceDate, Item,  Pounds) AS 
(
   SELECT 
    CONVERT(varchar(8), dbo.ufn_GetLastDayOfMonth(Invoice.InvoiceDate), 1) + ''' + ' Lbs.' + ''' As LastInvoiceDate,
    Item, 
    Pounds 
   FROM INVOICE
   WHERE InvoiceDate BETWEEN @BEGIN_DATE AND  @END_DATE 
)
SELECT *
FROM AllOrders
PIVOT
(
  SUM(QuantityShipped)
  FOR LastInvoiceDate  IN (' + @v_Columns + ')
) AS pivotview'

提前谢谢大家!

2 个答案:

答案 0 :(得分:2)

为了获得结果,您将需要PIVOT两次或将DollarsLbs列UNPIVOT到一个列中,然后应用PIVOT一次。我的偏好是不要转动然后转动,因为我发现它要容易得多。

不应先动态工作,而应将查询编写为静态或硬编码版本以使逻辑正确,然后将其转换为动态SQL。我的示例使用您的最终日期201-01-31等,因为您正在使用函数来创建这些日期,并且应该能够根据需要应用它。

由于您使用的是SQL Server 2005+,因此可以使用CROSS APPLY来取消DollarsLbs的移动。代码类似于以下内容:

select 
  t.ItemNo,
  new_col = convert(varchar(10), t.[invoice date], 120) + '_'+ c.col,
  c.value
from yourtable t
cross apply
(
  select 'Dollars', Dollars union all
  select 'Lbs', Lbs
) c (col, value);

SQL Fiddle with Demo。这会将您的数据转换为以下格式:

| ITEMNO |            NEW_COL | VALUE |
|--------|--------------------|-------|
|      A | 2014-01-31_Dollars |     1 |
|      A |     2014-01-31_Lbs |     1 |
|      B | 2014-01-31_Dollars |     2 |
|      B |     2014-01-31_Lbs |     2 |
|      A | 2014-01-31_Dollars |     3 |

我已将new_col连接到您需要的最终列名称。您可以再次使用您需要的任何格式设置日期格式,我只使用2014-01-31并将DollarsLbs添加到其末尾。获得数据后,您将把值存储到最终的预期结果中:

select ItemNo,
  [2014-01-31_Lbs], [2014-01-31_Dollars],
  [2014-02-28_Lbs], [2014-02-28_Dollars]
from
(
  select 
    t.ItemNo,
    new_col = convert(varchar(10), t.[invoice date], 120) + '_'+ c.col,
    c.value
  from yourtable t
  cross apply
  (
    select 'Dollars', Dollars union all
    select 'Lbs', Lbs
  ) c (col, value)
) d
pivot
(
  sum(value)
  for new_col in ([2014-01-31_Lbs], [2014-01-31_Dollars],
                  [2014-02-28_Lbs], [2014-02-28_Dollars])
) p;

SQL Fiddle with Demo。现在您已经获得了所需的结果,因此只需将其转换为动态SQL:

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

select @cols = STUFF((SELECT ',' + QUOTENAME(convert(varchar(10), t.[invoice date], 120) + '_'+ c.col) 
                    from yourtable t
                    cross apply
                    (
                      select 'Lbs', 0 union all
                      select 'Dollars', 1
                    ) c (col, so)
                    group by [invoice date], col, so
                    order by [invoice date], so
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')


set @query = 'SELECT ItemNo,' + @cols + ' 
            from 
            (
                select 
                  t.ItemNo,
                  new_col = convert(varchar(10), t.[invoice date], 120) + ''_''+ c.col,
                  c.value
                from yourtable t
                cross apply
                (
                  select ''Dollars'', Dollars union all
                  select ''Lbs'', Lbs
                ) c (col, value)
            ) d
            pivot 
            (
                sum(value)
                for new_col in (' + @cols + ')
            ) p '

exec sp_executesql @query;

SQL Fiddle with Demo。这给出了最终结果:

| ITEMNO | 2014-01-31_LBS | 2014-01-31_DOLLARS | 2014-02-28_LBS | 2014-02-28_DOLLARS |
|--------|----------------|--------------------|----------------|--------------------|
|      A |              4 |                  4 |              5 |                  5 |
|      B |              6 |                  6 |              6 |                  6 |

答案 1 :(得分:0)

这是您的示例表

  CREATE TABLE #TEMP([Invoice Date] DATE,[Item #] VARCHAR(10),[DollarS] NUMERIC(10,0),[Lbs.] NUMERIC(10,0))
    INSERT INTO #TEMP VALUES ('1/1/14', 'A',1,1)
    INSERT INTO #TEMP VALUES ('1/2/14', 'B',2,2)
    INSERT INTO #TEMP VALUES ('1/3/14', 'A',3,3)
    INSERT INTO #TEMP VALUES ('1/4/14', 'B',4,4)
    INSERT INTO #TEMP VALUES ('2/1/14', 'A',5,5)
    INSERT INTO #TEMP VALUES ('2/1/14', 'B',6,6)

现在您需要应用UNION ALL(而不是UNPIVOT)并将列添加到行并合并列,将列的顺序设为Date+LBS/DOLLARS

SELECT DISTINCT DENSE_RANK() OVER(ORDER BY  CAST(LASTDAY AS DATE),UNIT DESC)RNO,*,
CAST(DATEPART(MONTH,LASTDAY)AS VARCHAR) +'/'+ CAST(DATEPART(DAY,LASTDAY)AS VARCHAR) +'/' +RIGHT(CAST(YEAR(LASTDAY)AS VARCHAR),2)+' ' +UNIT  PIVOTCOL 
INTO #NEWTABLE 
FROM
(
    SELECT [Item #],'DOLLARS' UNIT,
    DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,[Invoice Date])+1,0))LASTDAY,
    SUM([Dollars]) OVER(PARTITION BY [Item #],DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,[Invoice Date])+1,0))) VALUE
    FROM #TEMP

    UNION ALL

    SELECT [Item #], 'LBS.',
    DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,[Invoice Date])+1,0))LASTDAY,
    SUM([Lbs.]) OVER(PARTITION BY [Item #],DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,[Invoice Date])+1,0))) DOLLARSUM
    FROM #TEMP
)TAB

现在声明查询以动态获取列并设置NULL to Zero

DECLARE @cols NVARCHAR (MAX)
DECLARE @NullToZeroCols NVARCHAR (MAX)

SELECT @cols = COALESCE (@cols + ',[' + PIVOTCOL + ']', 
              '[' + PIVOTCOL + ']')
               FROM    (SELECT DISTINCT RNO,PIVOTCOL FROM #NEWTABLE) PV  
               ORDER BY RNO
PRINT @COLS

SET @NullToZeroCols = SUBSTRING((SELECT ',ISNULL(['+PIVOTCOL+'],0) AS ['+PIVOTCOL+']' 
FROM(SELECT DISTINCT RNO,PIVOTCOL FROM #NEWTABLE GROUP BY RNO,PIVOTCOL)TAB  
ORDER BY RNO  FOR XML PATH('')),2,8000)

现在转动查询

DECLARE @query NVARCHAR(MAX)
SET @query = 'SELECT [Item #],' + @NullToZeroCols + ' FROM 
             (
                 SELECT [Item #],VALUE,PIVOTCOL FROM #NEWTABLE
             ) x
             PIVOT 
             (
                 SUM(VALUE)
                 FOR PIVOTCOL IN (' + @cols + ')
            ) p
            ORDER BY [Item #];' 

EXEC SP_EXECUTESQL @query

<强> RESULT

enter image description here