2015-04-22 48 views
1

我在SQL Server 2008環境中使用SQL Server Management Studio中工作2012年SQL多重動態查詢到一個查詢

我已經寫了3個獨立的查詢

查詢1

SQL查詢總結所有的現有存貨從Inventories

SELECT  StockCode, 
      Sum(QtyOnHand) AS 'SOH' 
FROM  InvWarehouse 
WHERE  StockCode NOT LIKE '%DEM%' AND StockCode NOT LIKE '%REF%' AND StockCode NOT LIKE 'Z%' 
GROUP BY InvWarehouse.StockCode 

查詢2

該查詢着眼於從採購訂單表未來的訂單,並動態返回下一個/ 12個月

DECLARE 
@cols AS NVARCHAR(MAX), 
@cols1 AS NVARCHAR(MAX), 
@cols2 AS NVARCHAR(MAX), 
@cols3 AS NVARCHAR(MAX), 
@query AS NVARCHAR(MAX) 

SELECT @cols = STUFF((SELECT ',' + QUOTENAME(YearMonth) 
        FROM 

       -- Selecting Using the Destinct -- 

        (SELECT DISTINCT CAST(YEAR([OrderDueDate]) AS NVARCHAR(4)) + RIGHT('00' + CAST(MONTH([OrderDueDate]) AS NVARCHAR(2)),2) AS YearMonth 
        FROM PorMasterHdr 
        JOIN PorMasterDetail 
        ON PorMasterDetail.PurchaseOrder = PorMasterHdr.PurchaseOrder 
        WHERE DATEDIFF(MONTH, OrderDueDate, DATEADD(m,12,GETDATE())) <= 12) sub 

        ORDER BY YearMonth 

        FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'),1,1,''), 

    @cols2 = STUFF((SELECT ',ISNULL(' + QUOTENAME(YearMonth) + ',0) AS ' + QUOTENAME(YearMonth) 
        FROM 

        -- Selecting Using the Destinct -- 

        (SELECT DISTINCT CAST(YEAR([OrderDueDate]) AS NVARCHAR(4)) + RIGHT('00' + CAST(MONTH([OrderDueDate]) AS NVARCHAR(2)),2) AS YearMonth 
        FROM PorMasterHdr 
        JOIN PorMasterDetail 
        ON PorMasterDetail.PurchaseOrder = PorMasterHdr.PurchaseOrder 
        WHERE DATEDIFF(MONTH, OrderDueDate, DATEADD(m,12,GETDATE())) <= 12) sub 

        ORDER BY YearMonth 

        FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'),1,1,'') 


SET @query = ' 
SELECT MStockCode, ' + @cols2 + ' 
FROM (
     SELECT  MStockCode, 
        MOrderQty, 
        CAST(YEAR([OrderDueDate]) AS NVARCHAR(4))+RIGHT(''00''+CAST(MONTH([OrderDueDate]) AS NVARCHAR(2)),2) AS YearMonth 
     FROM PorMasterHdr 
     JOIN PorMasterDetail 
     ON PorMasterDetail.PurchaseOrder = PorMasterHdr.PurchaseOrder 
     WHERE  MStockCode NOT LIKE ''%DEM%'' AND MStockCode NOT LIKE ''%REF%'' AND MStockCode NOT LIKE ''Z%'' 
     ) AS X 
PIVOT (
     SUM(MOrderQty) 
     FOR YearMonth in (' + @cols + ') 
     ) AS PT' 

EXECUTE (@query) 

QUERY 3

這個查詢着眼於過去12一個Sales表的銷售數據月份並動態返回最近/前12個月

DECLARE 
@cols AS NVARCHAR(MAX), 
@cols1 AS NVARCHAR(MAX), 
@cols2 AS NVARCHAR(MAX), 
@cols3 AS NVARCHAR(MAX), 
@query AS NVARCHAR(MAX) 

SELECT @cols = STUFF((SELECT ',' + QUOTENAME(YearMonth) 
        FROM 

       -- Selecting Using the Destinct -- 

        (SELECT DISTINCT CAST([TrnYear] AS NVARCHAR(4)) + RIGHT('00' + CAST([TrnMonth] AS NVARCHAR(2)),2) AS YearMonth 
        FROM ArTrnDetail 
        WHERE DATEDIFF(MONTH, InvoiceDate, GETDATE()) <= 12) sub 

        ORDER BY YearMonth 

        FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'),1,1,''), 

    @cols2 = STUFF((SELECT ',ISNULL(' + QUOTENAME(YearMonth) + ',0) AS ' + QUOTENAME(YearMonth) 
        FROM 

        -- Selecting Using the Destinct -- 

        (SELECT DISTINCT CAST([TrnYear] AS NVARCHAR(4)) + RIGHT('00' + CAST([TrnMonth] AS NVARCHAR(2)),2) AS YearMonth 
        FROM ArTrnDetail 
        WHERE DATEDIFF(MONTH, InvoiceDate, GETDATE()) <= 12) sub 

        ORDER BY YearMonth 

        FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'),1,1,'') 


SET @query = ' 
SELECT StockCode, ' + @cols2 + ' 
FROM (
     SELECT  StockCode, 
        QtyInvoiced, 
        CAST([TrnYear] AS NVARCHAR(4))+RIGHT(''00''+CAST([TrnMonth] AS NVARCHAR(2)),2) AS YearMonth 
     FROM  ArTrnDetail 
     WHERE  StockCode NOT LIKE ''%DEM%'' AND StockCode NOT LIKE ''%REF%'' AND StockCode NOT LIKE ''Z%'' 
     ) AS X 
PIVOT (
     SUM(QtyInvoiced) 
     FOR YearMonth in (' + @cols + ') 
     ) AS PT' 

EXECUTE (@query) 

每個查詢的結果都是正確的。現在我該如何將它們組合成一個查詢。以便他們返回

STOCKCODE  | Past 12 Month Sales Per Month | Stock On Hand | Future Purchases 

Helicopters | 1 4 5 2 3 4 6 1 3 2 3 2|   15  | 2 3 5 4 6 7 8 4 3 2 8 5 
Jam   | 2 5 6 4 8 5 8 5 7 2 1 2|   30  | 4 5 6 5 8 7 0 1 2 1 1 4 
Frogs   | 2 3 2 4 8 5 4 6 8 2 1 3|   7  | 5 7 8 8 6 7 4 0 1 2 1 2 

上面的股票代碼是來自不同表格的相同信息,例如,庫存中的直升機與採購訂單中的直升機相同。

+0

您可以創建存儲過程或創建一個SSRS報告產生上述報告。 – Hiten004

+0

糟糕的模式設計每年/每月都有單獨的列。此數據應位於單獨的表格中,其中包含年份和月份的列,原始表的主鍵/ ID的列和值的一列。如果你解決了這個問題,你可以將整個事件放到一個沒有動態sql的情況下運行。 –

+0

這是避免動態T-SQL的另一個原因。不過,您可能需要爲那些INTO臨時表執行結果,以便加入它們。 – Biscuits

回答

0

我建議如下:導致兩列

  1. 重寫@ QUERY2:StockCode和銷售。而不是選擇每個月作爲一個單獨的列,而是在VARCHAR中連接每個月。您已經爲@cols編寫了一個變量來單獨選擇列。保持旋轉。編寫一個變量(@SelSales),將每個月的結果連接到一個VARCHAR中,並將其用於Sales列的選擇。
  2. 將@ query3重寫爲兩列:StockCode和Purchases(類似於1)
  3. 將@ query1放入NVARCHAR(MAX)變量(選擇股票的變量)。
  4. 編寫一個@query將它們全部結合起來。對於@query

TSQL大綱:

DECLARE @query NVARCHAR(MAX); 
SET @query=N' 
SELECT 
    COALESCE(stock.StockCode,sales.StockCode,purchases.StockCode) AS StockCode, 
    COALESCE(sales.Sales,''0 0 0 0 0 0 0 0 0 0 0 0'') AS Sales, 
    COALESCE(stock.SOH,0) AS Stock, 
    COALESCE(purchases.Purchases,''0 0 0 0 0 0 0 0 0 0 0 0'') AS Purchases 
FROM 
    ('[email protected]+') AS stock 
    FULL JOIN ('[email protected]+') AS sales ON sales.StockCode=stock.StockCode 
    FULL JOIN ('[email protected]+') AS purchases ON purchases.StockCode=stock.StockCode'; 
EXEC(@query);