我正在写一个查询来获取数据,该查询基于VENDOR_ID
列中具有最小值的ITM_VNDR_PRIORITY
项。 ITM_VNDR_PRIORITY
来自另一个共享密钥VENDOR_ID
的表。
查询,我正在使用该查询来获取与某个项目相关联的供应商:
SELECT *
FROM PS_ITM_VENDOR
WHERE INV_ITEM_ID = '1003'
结果:
SETID INV_ITEM_ID VENDOR_SETID VENDOR_ID ITMV_PRIORITY_LOC ITM_ID_VNDR ITM_VNDR_PRIORITY
SHARE 1003 SHARE 84764A 000001 CFG246 3
SHARE 1003 SHARE CAMLAB 000001 CAT895 4
SHARE 1003 SHARE GYRENT 000001 TDOC-7FS 2
SHARE 1003 SHARE RLMED 000001 CAT895 1
我想为此ITM_VNDR_PRIORITY
(在本例中为VENDOR_ID“ RLMED”)中的SELECT *
FROM PS_ITM_VNDR_UOM UPDEV
WHERE INV_ITEM_ID = '1003'
最低的商品ID选择供应商,并且仅将该供应商的行返回到此查询中我当前返回的位置与该项目关联的所有供应商:
SETID INV_ITEM_ID VENDOR_SETID VENDOR_ID VNDR_LOC
--------------------------------------------------------
SHARE 1003 SHARE GYRENT 000001
SHARE 1003 SHARE RLMED 000001
SHARE 1003 SHARE 84764A 000001
SHARE 1003 SHARE CAMLAB 000001
SHARE 1003 SHARE GYRENT 000001
SHARE 1003 SHARE RLMED 000001
结果:
SELECT *
FROM PS_ITM_VNDR_UOM UPDEV
LEFT OUTER JOIN PS_ITM_VENDOR UPDEC ON UPDEV.SETID = UPDEC.SETID
AND UPDEV.INV_ITEM_ID = UPDEC.INV_ITEM_ID
AND UPDEV.VENDOR_SETID = UPDEC.VENDOR_SETID
AND UPDEV.VENDOR_ID = UPDEC.VENDOR_ID
WHERE UPDEV.INV_ITEM_ID = '1003'
我希望此查询仅基于(MIN)优先级最低的供应商返回与供应商“ RLMED”相关联的商品。到目前为止,这是我写的内容,因为我不确定如何仅获取与该项目关联的最小ITM_VNDR_PRIORITY的供应商ID。
SETID INV_ITEM_ID VENDOR_SETID VENDOR_ID VNDR_LOC
--------------------------------------------------------
SHARE 1003 SHARE RLMED 000001
SHARE 1003 SHARE RLMED 000001
在此示例中,期望的结果如下(返回具有最低(MIN)优先级的供应商“ RLMED”):
SELECT *
FROM PS_ITM_VNDR_UOM UPDEV
LEFT OUTER JOIN PS_ITM_VENDOR UPDEC ON UPDEV.SETID = UPDEC.SETID
AND UPDEV.INV_ITEM_ID = UPDEC.INV_ITEM_ID AND UPDEV.VENDOR_SETID = UPDEC.VENDOR_SETID AND UPDEV.VENDOR_ID = UPDEC.VENDOR_ID
WHERE UPDEV.INV_ITEM_ID = '1003'
AND EXISTS (SELECT MIN(ITM_VNDR_PRIORITY)
FROM PS_ITM_VENDOR UPDEC
WHERE UPDEC.INV_ITEM_ID = UPDEV.INV_ITEM_ID
AND UPDEC.VENDOR_ID = UPDEC.VENDOR_ID)
编辑:
我尝试使用此查询,但它仍返回所有供应商,而不是优先级最低的供应商
{{1}}
答案 0 :(得分:1)
我建议使用相关子查询:
SELECT UPDEV.*
FROM PS_ITM_VNDR_UOM UPDEV LEFT OUTER JOIN
PS_ITM_VENDOR UPDEC
ON UPDEV.SETID = UPDEC.SETID AND
UPDEV.INV_ITEM_ID = UPDEC.INV_ITEM_ID AND
UPDEV.VENDOR_SETID = UPDEC.VENDOR_SETID AND
UPDEV.VENDOR_ID = UPDEC.VENDOR_ID
WHERE UPDEV.INV_ITEM_ID = 1003 AND
UPDEC.ITM_VNDR_PRIORITY = (SELECT MIN(UPDEC2.ITM_VNDR_PRIORITY)
FROM PS_ITM_VENDOR UPDEC2
WHERE UPDEC2.SETID = UPDEC.SETID AND
UPDEC2.INV_ITEM_ID = UPDEC.INV_ITEM_ID AND
UPDEC2.VENDOR_SETID = UPDEC.VENDOR_SETID AND
UPDEC2.VENDOR_ID = UPDEC.VENDOR_ID
);
尤其是在PS_ITM_VNDR_UOM(INV_ITEM_ID, ITM_VNDR_PRIORITY)
上有索引的情况下,这应该非常快。