1

我有一个查询,我试图从查询特定 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 个特定值。

如果有人对此有任何见解,将不胜感激。非常感谢!

4

2 回答 2

1

I think you're saying you want to have a record returned, with the servicepointid column populated, but all others null?

In that case, use a union.

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...))

Here's a complete example:

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)
于 2012-04-10T20:16:53.097 回答
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 
于 2012-04-10T19:57:09.197 回答