我有一个查询,我试图从查询特定ID的表中提取一些值。如果该值不存在,我仍然希望查询返回一个只有我正在寻找的ID值的记录。这是我到目前为止所尝试的内容。
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个特定值。
如果有人对此有任何见解,我们将不胜感激。非常感谢!
答案 0 :(得分:1)
如果我理解正确,您只需将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
答案 1 :(得分:1)
我认为你说你想恢复一条记录,填充了servicepointid列,但所有其他列都是null?
在这种情况下,请使用联盟。
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)