当我合并包含DENSE_RANK()
的查询以生成IN
子句的输入时,我遇到了一些奇怪的结果。
证明我把它们分开了;
此查询
select *
from ALL_QUOTE
where ID in ('G002WMLS')
返回1个结果:
ID LongID StartDate EndDate
G002WMLS 67888 01/10/2011 30/11/2011
此查询
select ID
from (
select LongId, ID, DENSE_RANK() over (partition by LongId order by end_date desc, substr(ID, 2, 7) desc, start_date desc) d
from WithoutPD
)
where d = 1 and LongId = '67888'
也会返回1个结果:
ID
G002WMLS
然而,当我把它们组合在一起时:
select *
from ALL_QUOTE
where ID in (
select ID
from (
select LongId, ID, DENSE_RANK() over (partition by LongId order by end_date desc, substr(ID, 2, 7) desc, start_date desc) d
from WithoutPD
)
where d = 1
)
and LongId = '67888';
我最终得到了两个结果:
ID LongID StartDate EndDate
G002MIMQ 67888 01/10/2010 30/09/2011
G002WMLS 67888 01/10/2011 30/11/2011
我根本无法理解G002MIMQ
如何包含在结果中。我正在使用Oracle 11.2.0.1.0,但我知道这可能是一个我误解的通用SQL功能。
希望你能解释一下这个奇怪的问题。
答案 0 :(得分:1)
您已将and LongID='67888'
移到子查询的where子句之外。
请尝试此查询...
select *
from ALL_QUOTE
where ID in (
select ID
from (
select LongId, ID, DENSE_RANK() over (partition by LongId order by end_date desc, substr(ID, 2, 7) desc, start_date desc) d
from WithoutPD
)
where d = 1
and LongId = '67888'
);
修改强>
select
AllQuote.*
from
AllQuote
inner join
(
select ID
from (
select LongId, ID, DENSE_RANK() over (partition by LongId order by end_date desc, substring(ID, 2, 7) desc, start_date desc) d
from WithoutPD
) t
where d = 1
and LongId = '67888'
) v
on AllQuote.ID = v.ID