我有下表(X)。
pId | threshold | upperLimit | lowerLimit
-----------------------------------------------------
1 | 15 | 10 | 5
2 | 10 | 9 | 4
我有变量v_count, v_score, v_pId, v_pId2
。
目前,我们有以下查询。
select case when v_count >= threshold
then lowerLimit
else upperLimit end into v_score
from X
where pId = v_pId
现在我想根据要求得出结果
如果v_pId2
不为null,那么如果v_count
小于阈值,那么上层查询的where子句为where pId = v_pId
但如果v_count
大于阈值,那么where上的where子句查询为where pId = v_pId2
。
实施例,
Input(possible values of variable) | Output
---------------------------------------|-----------
v_pId | v_pId2 | v_count | v_score
-----------|--------------|------------|-----------
1 | 2 | 16 | 4
1 | 2 | 14 | 10
1 | null | 16 | 5
1 | null | 14 | 10
我怎样才能做到这一点?
我的方法是(不适合所有人)
1st run this query:-
select pId into v_pId
from X
where pId = case when v_pId2 is not null and v_count >= threshold
then v_pId2
else v_pId
group by pId;
Then run this query:-
select case when v_count >= threshold
then lowerLimit
else upperLimit end into v_score
from X
where pId = v_pId
还有其他方法吗?(使用单个查询)(修改表不是一个选项)