我有以下查询,
select Vendor_ID, Descr, source, Name, Parent_Vendor_ID from VENDOR;
数据:
VendorID Descr Source name Parent Vendor ID
1234 ONLINE ABC JACK 6666
5689 OFFSHORE XYZ SAM 5555
9999 SHOP TTT EBAY 1234
6666 ECOMMERCE FRE AMERICAS
从上表中,数据5555不存在于供应商ID字段中。所以我需要输出如下
VendorID Descr Source name Parent Vendor ID EXIST
1234 ONLINE ABC JACK 6666 Yes
5689 OFFSHORE XYZ SAM **5555 No**
9999 SHOP TTT EBAY 1234 Yes
6666 ECOMMERCE FRE AMERICAS
我需要查找字段1 Vendor_ID中是否存在Parent_Vendor_ID。如果存在则只需要一个带有是/否的列。如果是,则否则否
输出应为所有字段+ Parent_Vendor_ID_Exist
select Vendor_ID, Descr, source, Name, Parent_Vendor_ID,Parent_Vendor_ID Exist
我尝试了,否则它没有返回所有值。有人可以建议吗?
答案 0 :(得分:1)
select v1.Vendor_ID, v1.Descr, v1.source, v1.Name, v1.Parent_Vendor_ID,
case when v2.Vendor_ID is null
then 'NO'
else 'YES'
end as Parent_Vendor_ID_Exist
from vendor v1
left join vendor v2 on v1.Parent_Vendor_ID = v2.Vendor_ID
答案 1 :(得分:0)
SELECT v1.Vendor_ID
, v1.Descr
, v1.source
, v1.Name
, v1.Parent_Vendor_ID
, nvl2(v2.vendor_id,'Yes','No') as parent_exist
FROM VENDOR v1
LEFT OUTER JOIN vendor v2
ON (v1.parent_vandor_id = v2.vendor_id);
答案 2 :(得分:0)
试试这个..
SELECT Vendor_ID, Descr, source, Name, Parent_Vendor_ID,
CASE
WHEN
EXISTS( SELECT Vendor_ID FROM VENDOR v2 WHERE v1.Parent_Vendor_ID = v2.Vendor_ID)
THEN
'Yes'
ELSE
'No'
END as "Parent_Vendor_ID Exist"
FROM VENDOR v1;
让我知道这是否有效。