【发布时间】:2021-03-29 15:54:53
【问题描述】:
我正在使用两张桌子。一种是 [Receipt Cash Book] (RCB),另一种是银行支票。一些记录从 RCB 插入并插入到 BC。我编写了以下查询来查看 RCB 表中的记录
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 表中的记录
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 |
但我无法将参数值(硬编码值是参数化值)传递给查看。
你能帮我通过结合以上两个查询来解决这个问题吗?
【问题讨论】:
-
使用这段代码我没有优化它..我只是对你的代码进行了修改还有一个问题是你的代码没有优化。工作方式更多
标签: sql sql-server sql-server-2005