如何查找不存在另一个相关行的行
How to find rows for which another related row does not exist
我有一个 table,其中包含发票编号和交易代码。单张发票可以包含交易代码 10 和 11:
+------------------+-----------------+
| Transaction Code | Invoice Number |
+------------------+-----------------+
| 10 | CAN000700798 |
| 20 | CAN000700798 |
+------------------+-----------------+
发票可能只包含交易代码为 10 的记录:
+------------------+-----------------+
| Transaction Code | Invoice Number |
+------------------+-----------------+
| 10 | CAN000700798 |
+------------------+-----------------+
显示两种情况的发票示例 table:
+------------------+-----------------+
| Transaction Code | Invoice Number |
+------------------+-----------------+
| 10 | CAN000700798 |
+------------------+-----------------+
| 10 | CAN000900999 |
| 20 | CAN000900999 |
+------------------+-----------------+
| 10 | CAN000700777 |
| 20 | CAN000700777 |
+------------------+-----------------+
这是我对发票查询的期望 table;因为发票 CAN000700798 只包含交易代码 10 而不是 10 和 20。
+------------------+-----------------+
| Transaction Code | Invoice Number |
+------------------+-----------------+
| 10 | CAN000700798 |
+------------------+-----------------+
我需要编写一个查询来查看发票并检查它是否同时包含交易代码为 10 和 11 的记录。如果发票仅包含交易代码 10,return 记录。
如果以下查询适合您,请您试试:
SELECT T.TransactionCode, T.InvoiceNumber
FROM InvoiceTable AS T
WHERE T.TransactionCode = 10 AND NOT EXISTS (
SELECT 1
FROM InvoiceTable AS X
WHERE X.InvoiceNumber = T.InvoiceNumber AND X.TransactionCode <> 10
)
您可以使用 GROUP BY
和 HAVING
子句:
SELECT InvoiceNumber, MIN(TransactionCode) AS TransactionCode
FROM t
GROUP BY InvoiceNumber
HAVING MIN(TransactionCode) = 10
AND MAX(TransactionCode) = 10
我有一个 table,其中包含发票编号和交易代码。单张发票可以包含交易代码 10 和 11:
+------------------+-----------------+
| Transaction Code | Invoice Number |
+------------------+-----------------+
| 10 | CAN000700798 |
| 20 | CAN000700798 |
+------------------+-----------------+
发票可能只包含交易代码为 10 的记录:
+------------------+-----------------+
| Transaction Code | Invoice Number |
+------------------+-----------------+
| 10 | CAN000700798 |
+------------------+-----------------+
显示两种情况的发票示例 table:
+------------------+-----------------+
| Transaction Code | Invoice Number |
+------------------+-----------------+
| 10 | CAN000700798 |
+------------------+-----------------+
| 10 | CAN000900999 |
| 20 | CAN000900999 |
+------------------+-----------------+
| 10 | CAN000700777 |
| 20 | CAN000700777 |
+------------------+-----------------+
这是我对发票查询的期望 table;因为发票 CAN000700798 只包含交易代码 10 而不是 10 和 20。
+------------------+-----------------+
| Transaction Code | Invoice Number |
+------------------+-----------------+
| 10 | CAN000700798 |
+------------------+-----------------+
我需要编写一个查询来查看发票并检查它是否同时包含交易代码为 10 和 11 的记录。如果发票仅包含交易代码 10,return 记录。
如果以下查询适合您,请您试试:
SELECT T.TransactionCode, T.InvoiceNumber
FROM InvoiceTable AS T
WHERE T.TransactionCode = 10 AND NOT EXISTS (
SELECT 1
FROM InvoiceTable AS X
WHERE X.InvoiceNumber = T.InvoiceNumber AND X.TransactionCode <> 10
)
您可以使用 GROUP BY
和 HAVING
子句:
SELECT InvoiceNumber, MIN(TransactionCode) AS TransactionCode
FROM t
GROUP BY InvoiceNumber
HAVING MIN(TransactionCode) = 10
AND MAX(TransactionCode) = 10