连接两个表并需要在 ON 子句中具有 MAX 聚合函数
Joining two tables and need to have MAX aggregate function in ON clause
这是我的代码!我想在我的报告中提供零件 ID 和采购订单 ID,它会提供所有与这些规格相关的信息。重要的是,如果我们有相同的采购订单 ID 和零件 ID,我们需要代码 return 具有最高交易 ID 的结果。以下代码没有提供我所期望的。你能帮帮我吗?
SELECT MAX(INVENTORY_TRANS.TRANSACTION_ID), INVENTORY_TRANS.PART_ID
, INVENTORY_TRANS.PURC_ORDER_ID, TRACE_INV_TRANS.QTY, TRACE_INV_TRANS.CREATE_DATE, TRACE_INV_TRANS.TRACE_ID
FROM INVENTORY_TRANS
JOIN TRACE_INV_TRANS ON INVENTORY_TRANS.TRANSACTION_ID = TRACE_INV_TRANS.TRANSACTION_ID
WHERE INVENTORY_TRANS.PART_ID = @PartID
AND INVENTORY_TRANS.PURC_ORDER_ID = @PurchaseOrderID
GROUP BY TRACE_INV_TRANS.QTY, TRACE_INV_TRANS.CREATE_DATE, TRACE_INV_TRANS.TRACE_ID, INVENTORY_TRANS.PART_ID
, INVENTORY_TRANS.PURC_ORDER_ID
trace_inventory_transtable的样本是:
part_id trace_id transaction id qty create_date
x 1 10
x 2 11
x 3 12
inventory_transtable的样本是:
transaction_id part_id purc_order_id
11 x p20
12 x p20
我想获得最大交易的结果,即交易 12,但它显示交易 11
我会使用子查询来查找 MAX
值,然后将该结果与另一个 table.
ORDER BY
+ TOP (1)
returns transaction_id
的 MAX
值。
SELECT
inv.transaction_id
,inv.part_id
,inv.purc_order_id
,tr.qty
,tr.create_date
,tr.trace_id
FROM
(
SELECT TOP (1)
transaction_id,
part_id,
purc_order_id
FROM
INVENTORY_TRANS
WHERE
part_id = @PartID
AND
purc_order_id = @PurchaseOrderID
ORDER BY
transaction_id DESC
) AS inv
JOIN
TRACE_INV_TRANS AS tr
ON inv.transaction_id = tr.transaction_id;
结果:
+----------------+---------+---------------+------+-------------+----------+
| transaction_id | part_id | purc_order_id | qty | create_date | trace_id |
+----------------+---------+---------------+------+-------------+----------+
| 12 | x | p20 | NULL | NULL | 3 |
+----------------+---------+---------------+------+-------------+----------+
这是我的代码!我想在我的报告中提供零件 ID 和采购订单 ID,它会提供所有与这些规格相关的信息。重要的是,如果我们有相同的采购订单 ID 和零件 ID,我们需要代码 return 具有最高交易 ID 的结果。以下代码没有提供我所期望的。你能帮帮我吗?
SELECT MAX(INVENTORY_TRANS.TRANSACTION_ID), INVENTORY_TRANS.PART_ID
, INVENTORY_TRANS.PURC_ORDER_ID, TRACE_INV_TRANS.QTY, TRACE_INV_TRANS.CREATE_DATE, TRACE_INV_TRANS.TRACE_ID
FROM INVENTORY_TRANS
JOIN TRACE_INV_TRANS ON INVENTORY_TRANS.TRANSACTION_ID = TRACE_INV_TRANS.TRANSACTION_ID
WHERE INVENTORY_TRANS.PART_ID = @PartID
AND INVENTORY_TRANS.PURC_ORDER_ID = @PurchaseOrderID
GROUP BY TRACE_INV_TRANS.QTY, TRACE_INV_TRANS.CREATE_DATE, TRACE_INV_TRANS.TRACE_ID, INVENTORY_TRANS.PART_ID
, INVENTORY_TRANS.PURC_ORDER_ID
trace_inventory_transtable的样本是:
part_id trace_id transaction id qty create_date
x 1 10
x 2 11
x 3 12
inventory_transtable的样本是:
transaction_id part_id purc_order_id
11 x p20
12 x p20
我想获得最大交易的结果,即交易 12,但它显示交易 11
我会使用子查询来查找 MAX
值,然后将该结果与另一个 table.
ORDER BY
+ TOP (1)
returns transaction_id
的 MAX
值。
SELECT
inv.transaction_id
,inv.part_id
,inv.purc_order_id
,tr.qty
,tr.create_date
,tr.trace_id
FROM
(
SELECT TOP (1)
transaction_id,
part_id,
purc_order_id
FROM
INVENTORY_TRANS
WHERE
part_id = @PartID
AND
purc_order_id = @PurchaseOrderID
ORDER BY
transaction_id DESC
) AS inv
JOIN
TRACE_INV_TRANS AS tr
ON inv.transaction_id = tr.transaction_id;
结果:
+----------------+---------+---------------+------+-------------+----------+
| transaction_id | part_id | purc_order_id | qty | create_date | trace_id |
+----------------+---------+---------------+------+-------------+----------+
| 12 | x | p20 | NULL | NULL | 3 |
+----------------+---------+---------------+------+-------------+----------+