SQL对一个查询的多个动态查询

时间:2015-04-22 15:17:28

标签: sql sql-server-2008

我在使用SQL Server Management Studio 2012的SQL Server 2008环境中工作。

我写了3个单独的查询

QUERY 1

SQL查询SUMS来自Inventories

的所有STOCK ON HAND
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

QUERY 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

此查询查看Sales表中过去12个月的销售数据,并动态返回上一个/前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

以上的股票代码是来自不同表格的相同信息,例如。库存中的直升机与采购订单中的直升机相同。

1 个答案:

答案 0 :(得分:0)

我建议如下:

  1. 重写@ query2以产生两列:StockCode和Sales。而不是将每个月选择为单独的列,而是在VARCHAR中连接每个月。您已经为@cols写了一个变量来单独选择列。保持旋转。编写一个变量(@SelSales)以在VARCHAR中连接每个月的结果,并在您选择的Sales列中使用它。
  2. 重写@ query3以产生两列:StockCode和Purchases(类似于1。)
  3. 将@ query1放入NVARCHAR(MAX)变量(选择股票的变量)。
  4. 写一个@query将它们全部组合起来。
  5. @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
      ('+@query1+') AS stock
      FULL JOIN ('+@query2+') AS sales ON sales.StockCode=stock.StockCode
      FULL JOIN ('+@query3+') AS purchases ON purchases.StockCode=stock.StockCode';
    EXEC(@query);