1

我有一个存储过程和一个获取日期范围查询。我想加入它只显示一个结果集的两个查询。程序和查询如下..

CREATE PROCEDURE ak_ReportData_CMTDB

  @BankAccountID uniqueidentifier,  
  @StartDate     datetime,  
  @EndDate       datetime  

AS  

----------------------------------------------------------------------------  
-- Name:     ak_ReportData_CMTDB  
-- Purpose:  Returns the daily activity on the passed bank account for the   
--           passed date range  
-- Input:    BankAccountID - The bank account to report on  
--           StartDate, EndDate - The date range to report on  
-- Output:   Recordset containing activity  
-- Return:   0 = Sucess  
--           >0 = Error  
----------------------------------------------------------------------------  

SET NOCOUNT ON  

DECLARE @OpeningBalance money  

-- Calculate opening balance for deposits  
SELECT NULL AS fDepositDate,  
       SUM(CASE fType  
            WHEN 0 THEN fAmount                    -- Deposit (Add amount)  
            WHEN 1 THEN (CASE fSide   
                          WHEN 1 THEN fAmount * -1 -- Transfer Out (Subtract amount)  
                          WHEN 2 THEN fAmount      -- Transfer In (Add amount)  
                                 ELSE 0   
                         END)  
            WHEN 2 THEN fAmount                    -- Credit Card (Add amount)  
            WHEN 3 THEN fAmount * -1               -- Withdrawal (Subtract amount)  
                   ELSE 0  
           END) As fNetChange  
FROM tBADepositMaster   
WHERE fBankAccountID = @BankAccountID  
  AND fDepositDate < @StartDate   
  AND fVoid = 0  

UNION ALL  

-- Calculate NetChange for deposits for each date  
SELECT fDepositDate,  
       SUM(CASE fType  
            WHEN 0 THEN fAmount                    -- Deposit (Add amount)  
            WHEN 1 THEN (CASE fSide   
                          WHEN 1 THEN fAmount * -1 -- Transfer Out (Subtract amount)  
                          WHEN 2 THEN fAmount      -- Transfer In (Add amount)  
                                 ELSE 0   
                         END)  
            WHEN 2 THEN fAmount                    -- Credit Card (Add amount)  
            WHEN 3 THEN fAmount * -1               -- Withdrawal (Subtract amount)  
                   ELSE 0  
           END) AS fNetChange  
FROM tBADepositMaster   
WHERE fBankAccountID = @BankAccountID  
  AND fDepositDate BETWEEN @StartDate AND @EndDate  
  AND fVoid = 0  
GROUP BY fDepositDate 

UNION ALL

-- Calculate opening balance for checks  
SELECT NULL As fDepositDate,  
       SUM(fAmount) * -1 AS fNetChange  
FROM tBACheck  
WHERE fBankAccountID = @BankAccountID  
  AND fDate < @StartDate   
  AND fVoid = 0  

UNION ALL  

-- Calculate NetChange for checks for each date  
SELECT fDate As fDepositDate,  
       SUM(fAmount) * -1 AS fNetChange  
FROM tBACheck  
WHERE fBankAccountID = @BankAccountID  
  AND fDate BETWEEN @StartDate AND @EndDate  
  AND fVoid = 0  
GROUP BY fDate  
ORDER BY fDepositDate  

并获取日期范围查询

;WITH tmpinfo(tmpdt) AS 
(SELECT @StartDate 
UNION ALL 
SELECT tmpdt + 1 
FROM tmpinfo 
WHERE tmpdt < @EndDate ) 
SELECT * FROM tmpinfo OPTION (MAXRECURSION 0);

是否可以在单个查询中加入这两个查询?

4

1 回答 1

0

通过一个结果集,您的意思是您希望将第二个查询结果作为附加列添加到第一个查询中。如果是,那么您可以使用 join 将两个查询与公共列结合起来。

于 2013-07-24T15:20:09.537 回答