Oracle SQL:即使特定值不存在,也会返回记录';不存在
我有一个查询,试图从查询特定ID的表中提取一些值。如果该值不存在,我仍然希望查询返回一条记录,该记录只包含我要查找的ID值。这是我到目前为止试过的Oracle SQL:即使特定值不存在,也会返回记录';不存在,sql,oracle,join,Sql,Oracle,Join,我有一个查询,试图从查询特定ID的表中提取一些值。如果该值不存在,我仍然希望查询返回一条记录,该记录只包含我要查找的ID值。这是我到目前为止试过的 Select attr.attrval, attr.uidservicepoint, sp.servicepointid From bilik.lssrvcptmarketattr attr Join bilik.lsmarketattrtype type on attr.uidmarketattrtype = type.uidmarketatt
Select attr.attrval, attr.uidservicepoint, sp.servicepointid
From bilik.lssrvcptmarketattr attr
Join bilik.lsmarketattrtype type on attr.uidmarketattrtype = type.uidmarketattrtype AND
type.attrtype IN ('CAPACITY_REQUIREMENT_KW') and TO_CHAR( attr.starttime , 'mm/dd/yyyy')in ('05/01/2011')
Right Outer Join bilik.lsservicepoint sp on attr.uidservicepoint = sp.uidservicepoint
Where sp.servicepointid in ('RGE_R01000051574382') Order By sp.servicepointid ASC
在本例中,我试图查找RGE_R01000051574382。如果表SP.servicepointid中不存在该值,我希望它仍然在记录中返回“RGE_R01000051574382”,并为我正在提取的其他值返回空值。通常,当我运行这个程序时,我会一次提取大约1000个特定值
如果有人对此有任何见解,我们将不胜感激。非常感谢 如果我理解正确,您只需要将
WHERE
子句移动到JOIN
子句中
select attr.attrval,
attr.uidservicepoint,
sp.servicepointid
from bilik.lssrvcptmarketattr attr
join bilik.lsmarketattrtype type on attr.uidmarketattrtype = type.uidmarketattrtype
and type.attrtype in ('CAPACITY_REQUIREMENT_KW')
and TO_CHAR(attr.starttime, 'mm/dd/yyyy') in ('05/01/2011')
right outer join bilik.lsservicepoint sp on attr.uidservicepoint = sp.uidservicepoint
and sp.servicepointid in ('RGE_R01000051574382')
order by sp.servicepointid
我想您是说希望返回一条记录,并填充servicepointid列,但其他所有记录都为空 在这种情况下,使用工会
select ...your query without order by...
and sp.servicepointid = 'RGE_R010000515743282'
union
select null, null, 'RGE_R010000515743282'
from dual
where not exists (select 'x' from (...your query without order by...))
下面是一个完整的示例:
create table test (id number, val varchar2(10));
insert into test (id, val) values (1, 'hi');
select id,
val
from test
where id = 1
union
select 1,
null
from dual
where not exists (select 'x'
from test
where id = 1)
我想可能也是这样,所以我尝试了一下,但所做的只是返回“sp.servicepointid”中的每一条记录。我想如果我每次只查找一个servicepointid,这可能会起作用,但实际上我每次查找的特定值大约为1000。所以,我会寻找“RGE_R010000515743282和RGE_R010000515743283和RGE_R010000515743284,等等……在这种情况下,我可能会寻找一个流水线表函数,每次找不到一个ID时,你都会通过管道传输一个稀疏行……或者使用一个全局临时表来存储你的“搜索ID”和外部连接。