使用连接或任何其他方法连接两个查询?

Connect two queries using join or any other method?

我正在使用两个 table。一种是 [Receipt Cash Book] (RCB),另一种是银行支票。一些记录从 RCB 插入并插入 BC。我写了以下查询来查看 RCB table

中的记录
SELECT RCBBankCode, RCBBranchCode, RCBChequeDate, RCBChequeNo, SUM(RCBOrginalAmount) AS ChqAmount
FROM         dbo.[Receipt Cash Book]
WHERE     (RCBLocationCode = '01') AND (RCBReceiptDate = CONVERT(date, '20200918', 112)) AND (RCBCancelTag = 0) AND (RCBPaymentCode <> 'CASH')
GROUP BY RCBBankCode, RCBBranchCode, RCBChequeDate, RCBChequeNo

我写了以下查询来查看 BC 中的记录 table

    SELECT     DepQChqBank, DepQChqBranch, DepQChqDate, DepQChqNo, CASE WHEN EXISTS
                          (SELECT DISTINCT 
                                                   [Banking Cheques].DepQDate, [Banking Cheques].DepQBank, [Banking Cheques].DepQBranch, [Banking Cheques].DepQAccountNo, 
                                                   [Banking Cheques].DepQRLocation, [Banking Cheques].DepQRDate, [Banking Cheques].DepQChqBank, 
                                                   [Banking Cheques].DepQChqBranch, [Banking Cheques].DepQChqDate, [Banking Cheques].DepQChqNo
                            FROM          [Banking Cheques] INNER JOIN
                                                   [Receipt Cash Book] ON [Banking Cheques].DepQRLocation = [Receipt Cash Book].RCBLocationCode AND 
                                                   [Banking Cheques].DepQRDate = [Receipt Cash Book].RCBReceiptDate AND 
                                                   [Banking Cheques].DepQChqBank = [Receipt Cash Book].RCBBankCode AND 
                                                   [Banking Cheques].DepQChqBranch = [Receipt Cash Book].RCBBranchCode AND 
                                                   [Banking Cheques].DepQChqDate = [Receipt Cash Book].RCBChequeDate AND 
                                                   [Banking Cheques].DepQChqNo = [Receipt Cash Book].RCBChequeNo
                            WHERE      ([Banking Cheques].DepQBank = '7010') AND ([Banking Cheques].DepQBranch = '660') AND 
                                                   ([Banking Cheques].DepQAccountNo = '0000000502') AND ([Banking Cheques].DepQRLocation = '01') AND 
                                                   ([Banking Cheques].DepQRDate = CONVERT(date, '20200918', 112))) THEN 1 ELSE 0 END AS Selected
FROM         dbo.[Banking Cheques]

从这两个查询中我得到了我预期的结果。 第一个查询结果

RCBBankCode RCBBranchCode RCBChequeDate RCBChequeNo ChqAmount
7010 002 2020-09-13 963147 5692.50
7010 002 2020-09-18 123456 5376.25
7056 004 2020-09-14 963789 6000.00

第二条查询结果

DepQChqBank DepQChqBranch DepQChqDate DepQChqNo Selected
7056 004 2020-09-14 963789 1

但不是来自一个查询。我使用这两个查询创建视图并编写了一个查询并得到了我的结果。

    SELECT     vwRCB_BankingSelectReceipts.RCBBankCode, vwRCB_BankingSelectReceipts.RCBBranchCode, vwRCB_BankingSelectReceipts.RCBChequeDate, 
                      vwRCB_BankingSelectReceipts.RCBChequeNo, vwRCB_BankingSelectReceipts.ChqAmount, ISNULL(vwRCB_BankingChequesSelect.Selected, 0) 
                      AS Selected
FROM         vwRCB_BankingChequesSelect RIGHT OUTER JOIN
                      vwRCB_BankingSelectReceipts ON vwRCB_BankingChequesSelect.DepQRDate = vwRCB_BankingSelectReceipts.RCBReceiptDate AND 
                      vwRCB_BankingChequesSelect.DepQRLocation = vwRCB_BankingSelectReceipts.RCBLocationCode AND 
                      vwRCB_BankingChequesSelect.DepQChqBank = vwRCB_BankingSelectReceipts.RCBBankCode AND 
                      vwRCB_BankingChequesSelect.DepQChqBranch = vwRCB_BankingSelectReceipts.RCBBranchCode AND 
                      vwRCB_BankingChequesSelect.DepQChqDate = vwRCB_BankingSelectReceipts.RCBChequeDate AND 
                      vwRCB_BankingChequesSelect.DepQChqNo = vwRCB_BankingSelectReceipts.RCBChequeNo
WHERE     (vwRCB_BankingSelectReceipts.RCBLocationCode = '01') AND (vwRCB_BankingSelectReceipts.RCBReceiptDate = CONVERT(date, '20200918', 112))
ORDER BY vwRCB_BankingSelectReceipts.RCBBankCode, vwRCB_BankingSelectReceipts.RCBBranchCode

上述查询的结果是

RCBBankCode RCBBranchCode RCBChequeDate RCBChequeNo ChqAmount Selected
7010 002 2020-09-13 963147 5692.50 0
7010 002 2020-09-18 123456 5376.25 0
7056 004 2020-09-14 963789 6000.00 1

但是我无法传递参数值(硬编码值是参数化值)来查看。

你能帮我结合以上两个查询来解决这个问题吗?

使用此代码 我没有优化它..我只是修改了你的代码

还有一个问题是您的代码没有优化。更多工作方式

select *
from 
    (SELECT RCBBankCode, RCBBranchCode, RCBChequeDate, RCBChequeNo, SUM(RCBOrginalAmount) AS ChqAmount
    FROM         dbo.[Receipt Cash Book]
    WHERE     (RCBLocationCode = '01') AND (RCBReceiptDate = CONVERT(date, '20200918', 112)) AND (RCBCancelTag = 0) AND (RCBPaymentCode <> 'CASH')
    GROUP BY RCBBankCode, RCBBranchCode, RCBChequeDate, RCBChequeNo) Query1 RIGHT JOIN


    (SELECT     DepQChqBank, DepQChqBranch, DepQChqDate, DepQChqNo, CASE WHEN EXISTS
                          (SELECT DISTINCT 
                                                   [Banking Cheques].DepQDate, [Banking Cheques].DepQBank, [Banking Cheques].DepQBranch, [Banking Cheques].DepQAccountNo, 
                                                   [Banking Cheques].DepQRLocation, [Banking Cheques].DepQRDate, [Banking Cheques].DepQChqBank, 
                                                   [Banking Cheques].DepQChqBranch, [Banking Cheques].DepQChqDate, [Banking Cheques].DepQChqNo
                            FROM          [Banking Cheques] INNER JOIN
                                                   [Receipt Cash Book] ON [Banking Cheques].DepQRLocation = [Receipt Cash Book].RCBLocationCode AND 
                                                   [Banking Cheques].DepQRDate = [Receipt Cash Book].RCBReceiptDate AND 
                                                   [Banking Cheques].DepQChqBank = [Receipt Cash Book].RCBBankCode AND 
                                                   [Banking Cheques].DepQChqBranch = [Receipt Cash Book].RCBBranchCode AND 
                                                   [Banking Cheques].DepQChqDate = [Receipt Cash Book].RCBChequeDate AND 
                                                   [Banking Cheques].DepQChqNo = [Receipt Cash Book].RCBChequeNo
                            WHERE      ([Banking Cheques].DepQBank = '7010') AND ([Banking Cheques].DepQBranch = '660') AND 
                                                   ([Banking Cheques].DepQAccountNo = '0000000502') AND ([Banking Cheques].DepQRLocation = '01') AND 
                                                   ([Banking Cheques].DepQRDate = CONVERT(date, '20200918', 112))) THEN 1 ELSE 0 END AS Selected
        FROM         dbo.[Banking Cheques] ) Query2 
                                                        ON Query1.DepQRDate = Query2.RCBReceiptDate AND 
                                                           Query1.DepQRLocation = Query2.RCBLocationCode AND 
                                                           Query1.DepQChqBank = Query2.RCBBankCode AND 
                                                           Query1.DepQChqBranch = Query2.RCBBranchCode AND 
                                                           Query1.DepQChqDate = Query2.RCBChequeDate AND 
                                                           Query1.DepQChqNo = Query2.RCBChequeNo


WHERE     (Query2.RCBLocationCode = '01') AND (Query2.RCBReceiptDate = CONVERT(date, '20200918', 112))
ORDER BY Query2.RCBBankCode, Query2.RCBBranchCode