查询适用于具有较大表子集的表,但不适用于较大表

时间:2019-04-04 15:02:29

标签: sql sql-server

有一个很大的交易表。我将特定项目的所有事务复制到另一个表(称为TransactionsTest),该表具有相同的确切列供我测试。

交易记录表如下:

+----------------+--------------+---------------------+-------------+----------------------+
| Transaction_ID | Inventory_ID |   Transaction_DT    | Location_ID | Transaction_Quantity |
+----------------+--------------+---------------------+-------------+----------------------+
|          39993 |          302 | 2017-01-26 12:41:00 |           1 |                    2 |
|          39964 |          112 | 2017-01-26 9:55:00  |          20 |                    1 |
|          39890 |          417 | 2017-01-20 9:54:00  |           3 |                    5 |
|          39794 |          302 | 2017-01-11 9:50:00  |           3 |                    3 |
|          39790 |          302 | 2017-01-10 10:45:00 |          20 |                    2 |
|          39681 |          225 | 2017-01-02 8:32:00  |          20 |                    4 |
|          39682 |          227 | 2017-01-02 8:15:00  |           1 |                    1 |
|          39679 |          302 | 2017-01-01 11:23:00 |           3 |                    6 |
|          39599 |          302 | 2016-12-11 10:21:00 |           1 |                    2 |
+----------------+--------------+---------------------+-------------+----------------------+

因此,仅包含特定项目交易的TransactionsTest表如下:

+----------------+--------------+---------------------+-------------+----------------------+
| Transaction_ID | Inventory_ID |   Transaction_DT    | Location_ID | Transaction_Quantity |
+----------------+--------------+---------------------+-------------+----------------------+
|          39993 |          302 | 2017-01-26 12:41:00 |           1 |                    2 |
|          39794 |          302 | 2017-01-11 9:50:00  |           3 |                    3 |
|          39790 |          302 | 2017-01-10 10:45:00 |          20 |                    2 |
|          39679 |          302 | 2017-01-01 11:23:00 |           3 |                    6 |
|          39599 |          302 | 2016-12-11 10:21:00 |           1 |                    2 |
+----------------+--------------+---------------------+-------------+----------------------+

我建立了2个不同的查询,仅将最新的事务返回到每个位置。这是第一个:

SELECT t1.*
FROM TransactionsTest t1
WHERE t1.Transaction_DT = (SELECT MAX(t2.Transaction_DT)
                           FROM TransactionsTest t2
                           WHERE t2.Location_ID = t1.Location_ID)
AND Inventory_ID = 302 ORDER BY t1.Transaction_DT

这是第二个:

SELECT t1.*
FROM TransactionsTest t1
WHERE t1.Transaction_ID = (SELECT TOP 1 t2.Transaction_ID
                           FROM TransactionsTest t2
                           WHERE t2.Location_ID = t1.Location_ID
                           ORDER BY t2.Transaction_ID DESC)
AND Inventory_ID = 302 ORDER BY t1.Transaction_DT

两个查询在查询TransactionsTest时都返回此结果:

+----------------+--------------+---------------------+-------------+----------------------+
| Transaction_ID | Inventory_ID |   Transaction_DT    | Location_ID | Transaction_Quantity |
+----------------+--------------+---------------------+-------------+----------------------+
|          39993 |          302 | 2017-01-26 12:41:00 |           1 |                    2 |
|          39794 |          302 | 2017-01-11 9:50:00  |           3 |                    3 |
|          39790 |          302 | 2017-01-10 10:45:00 |          20 |                    2 |
+----------------+--------------+---------------------+-------------+----------------------+

但是当我在实际的事务表上运行相同的查询时,像这样:

SELECT t1.*
FROM Transactions t1
WHERE t1.Transaction_DT = (SELECT MAX(t2.Transaction_DT)
                           FROM Transactions t2
                           WHERE t2.Location_ID = t1.Location_ID)
AND Inventory_ID = 302 ORDER BY t1.Transaction_DT

或者这个:

SELECT t1.*
FROM Transactions t1
WHERE t1.Transaction_ID = (SELECT TOP 1 t2.Transaction_ID
                           FROM Transactions t2
                           WHERE t2.Location_ID = t1.Location_ID
                           ORDER BY t2.Transaction_ID DESC)
AND Inventory_ID = 302 ORDER BY t1.Transaction_DT

结果集是空的,像这样:

+----------------+--------------+----------------+-------------+----------------------+
| Transaction_ID | Inventory_ID | Transaction_DT | Location_ID | Transaction_Quantity |
+----------------+--------------+----------------+-------------+----------------------+
|                |              |                |             |                      |
+----------------+--------------+----------------+-------------+----------------------+

任何人都可以告诉我们这里发生了什么吗?为什么查询只在包含特定项目交易的表上返回预期结果?我唯一能想到的是它可能与原始事务表的创建方式有关,因为我没有复制所有约束/外键等。

1 个答案:

答案 0 :(得分:1)

库存条件也必须在子查询中:

WHERE t1.Transaction_ID = (SELECT TOP 1 t2.Transaction_ID
                           FROM TransactionsTest t2
                           WHERE t2.Location_ID = t1.Location_ID AND
                                 t2.Inventory_ID = t.Inventory_ID
                           ORDER BY t2.Transaction_ID DESC
                          )

否则,最近的事务可能具有Inventory_ID的另一个值。