我有一个存储过程和一个获取日期范围查询。我想加入它只显示一个结果集的两个查询。程序和查询如下..
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);
是否可以在单个查询中加入这两个查询?