create table tq84_range (
range_id number not null,
range_seq number not null,
min_ number not null,
max_ number,
factor number not null,
--
primary key (range_id, range_seq)
);
insert into tq84_range values (1, 1, 0, 10, 1.0);
insert into tq84_range values (1, 2, 10, 100, 1.5);
insert into tq84_range values (1, 3,101,null, 2.5);
insert into tq84_range values (2, 1, 0, 18, 1.0);
insert into tq84_range values (2, 2, 19,null, 2.0);
create table tq84_application (
app_id number not null,
raw_value number not null,
range_id number not null,
primary key (app_id)
);
insert into tq84_application values (1, 20.0, 1);
insert into tq84_application values (2, 25.0, 2);
insert into tq84_application values (3, 18.5, 2);
您想要使用left join
。
有了这样一个左连接,可以确保左 表的每个记录(在 select语句文本之前left join
出现的表),将至少有一次回来, 即使where条件没有按在右表中找不到记录 。
如果tq84_range.range is null
那么你知道加入 条件没有在tq84_range中找到记录,因此 似乎是一个缺口。所以你打印Missing:
。
由于tq84_application.max_
可以null
和空似乎 表示无穷或上限你测试上限 与nvl(tq84_range.max_, tq84_application.raw_value
因此,select语句会变成这样:
select
case when tq84_range.range_id is null then 'Missing: '
else ' '
end,
tq84_application.raw_value
from
tq84_application left join
tq84_range
on
tq84_application.range_id = tq84_range.range_id
and
tq84_application.raw_value between
tq84_range.min_ and nvl(tq84_range.max_, tq84_application.raw_value);
'我不需要sql本身,只是一些指向这个方法的指针。' – 2011-12-19 22:27:27
有些事情更容易用SQL来说明然后用英文拼写出来。 – Sparky 2011-12-19 22:29:49
这也包括范围内的值。对于范围“1 - 10”和“11 - 20”,我只需要那些介于10和11之间的值。所以我想'10.5'但不是'5'。 – Nivas 2011-12-19 22:35:35