2016-01-11 25 views
1

我創建了一個存儲過程,用於從表中提取數據並根據一天中出現的次數顯示彙總信息。這些列是動態的。具有兩個結果集的SQL PIVOT - 按日期對結果列進行重新排序

按日期降序排列列的最佳方法是什麼?

查詢:

--Declare Query 
DECLARE @DynamicPivotQuery AS NVARCHAR(MAX) 

--Declare COUNT variables 
DECLARE @ColumnNameCOUNT AS NVARCHAR(MAX) 
DECLARE @ColumnNameAgregateCOUNT AS NVARCHAR(MAX) 

--Declare SUM variables 
DECLARE @ColumnNameSUM AS NVARCHAR(MAX) 
DECLARE @ColumnNameAgregateSUM AS NVARCHAR(MAX) 

--Get distinct COUNT values of the PIVOT Column 
SELECT @ColumnNameCOUNT= ISNULL(@ColumnNameCOUNT + ',','') + QUOTENAME([ReportDate]) 
FROM (SELECT DISTINCT [ReportDate] FROM [tbBackordersArchive]) AS BackorderDates 

--Get summed COUNT values of the PIVOT Column 
SELECT @ColumnNameAgregateCOUNT = COALESCE(@ColumnNameAgregateCOUNT + ', SUM(','Sum(')+ QUOTENAME([ReportDate]) + ') as '+ QUOTENAME([ReportDate]) 
FROM (SELECT DISTINCT [ReportDate] FROM [tbBackordersArchive]) AS BackorderDates 

--Get distinct SUM values of the PIVOT Column 
SELECT @ColumnNameSUM= ISNULL(@ColumnNameSUM + ',','') + QUOTENAME([ReportDate]) 
FROM (SELECT DISTINCT [ReportDate] FROM [tbBackordersArchive]) AS BackorderDates 

--Get summed SUM values of the PIVOT Column 
SELECT @ColumnNameAgregateSUM = COALESCE(@ColumnNameAgregateSUM + ', SUM(','Sum(')+ QUOTENAME([ReportDate]) + ') as '+ QUOTENAME([ReportDate]) 
FROM (SELECT DISTINCT [ReportDate] FROM [tbBackordersArchive]) AS BackorderDates 

--Prepare the PIVOT query using the dynamic 
SET @DynamicPivotQuery = 
    N'SELECT ' + @ColumnNameAgregateCOUNT + ' 
    FROM [tbBackordersArchive] 
    PIVOT(COUNT(OrderedQuantity) 
      FOR [ReportDate] IN (' + @ColumnNameCOUNT + ')) AS PVTTable WHERE [Warehouse] = ''01'' 

    UNION ALL 

    SELECT ' + @ColumnNameAgregateSUM + ' 
    FROM [tbBackordersArchive] 
    PIVOT(SUM(OrderedQuantity) 
      FOR [ReportDate] IN (' + @ColumnNameSUM + ')) AS PVTTable WHERE [Warehouse] = ''01''' 

--Execute the Dynamic Pivot Query 
EXEC sp_executesql @DynamicPivotQuery 

表結構:

CREATE TABLE [dbo].[tbBackordersArchive](
[ReportDate] [date] NOT NULL, 
[Warehouse] [nvarchar](4) NULL, 
[PartNumber] [nvarchar](26) NULL, 
[Description] [nvarchar](37) NULL, 
[PDCStock] [float] NULL, 
[Account] [nvarchar](10) NULL, 
[AccountName] [nvarchar](80) NULL, 
[CreditHold] [nvarchar](3) NULL, 
[CustomerRef] [nvarchar](22) NULL, 
[WIP] [int] NULL, 
[DateEdit] [nvarchar](12) NULL, 
[OrderedQuantity] [float] NULL, 
[CumulativeQuantity] [float] NULL, 
[OrderType] [nvarchar](3) NULL 
) ON [PRIMARY] 
+0

按照設置包含新的透視列名稱的變量的日期排序。 – Rabbit

+0

謝謝HEAPS兔子! :) – Mike

回答

0

兔建議這裏是解析的代碼(請參閱ORDER BY在@ColumnNameAgregateSUM和@ColumnNameAgregateCOUNT:

--Declare Query 
DECLARE @DynamicPivotQuery AS NVARCHAR(MAX) 

--Declare COUNT variables 
DECLARE @ColumnNameCOUNT AS NVARCHAR(MAX) 
DECLARE @ColumnNameAgregateCOUNT AS NVARCHAR(MAX) 

--Declare SUM variables 
DECLARE @ColumnNameSUM AS NVARCHAR(MAX) 
DECLARE @ColumnNameAgregateSUM AS NVARCHAR(MAX) 

--Get distinct COUNT values of the PIVOT Column 
SELECT @ColumnNameCOUNT= ISNULL(@ColumnNameCOUNT + ',','') + QUOTENAME([ReportDate]) 
FROM (SELECT DISTINCT [ReportDate] FROM [tbBackordersArchive]) AS BackorderDates 

--Get summed COUNT values of the PIVOT Column 
SELECT @ColumnNameAgregateCOUNT = COALESCE(@ColumnNameAgregateCOUNT + ', SUM(','Sum(')+ QUOTENAME([ReportDate]) + ') as '+ QUOTENAME([ReportDate]) 
FROM (SELECT DISTINCT [ReportDate] FROM [tbBackordersArchive]) AS BackorderDates ORDER BY [ReportDate] DESC 

--Get distinct SUM values of the PIVOT Column 
SELECT @ColumnNameSUM= ISNULL(@ColumnNameSUM + ',','') + QUOTENAME([ReportDate]) 
FROM (SELECT DISTINCT [ReportDate] FROM [tbBackordersArchive]) AS BackorderDates 

--Get summed SUM values of the PIVOT Column 
SELECT @ColumnNameAgregateSUM = COALESCE(@ColumnNameAgregateSUM + ', SUM(','Sum(')+ QUOTENAME([ReportDate]) + ') as '+ QUOTENAME([ReportDate]) 
FROM (SELECT DISTINCT [ReportDate] FROM [tbBackordersArchive]) AS BackorderDates ORDER BY [ReportDate] DESC 

--Prepare the PIVOT query using the dynamic 
SET @DynamicPivotQuery = 
    N'SELECT ' + @ColumnNameAgregateCOUNT + ' 
    FROM [tbBackordersArchive] 
    PIVOT(COUNT(OrderedQuantity) 
      FOR [ReportDate] IN (' + @ColumnNameCOUNT + ')) AS PVTTable WHERE [Warehouse] = ''01'' 

    UNION ALL 

    SELECT ' + @ColumnNameAgregateSUM + ' 
    FROM [tbBackordersArchive] 
    PIVOT(SUM(OrderedQuantity) 
      FOR [ReportDate] IN (' + @ColumnNameSUM + ')) AS PVTTable WHERE [Warehouse] = ''01''' 

--Execute the Dynamic Pivot Query 
EXEC sp_executesql @DynamicPivotQuery