TABLE : **PROFILE**
RUN_ID| Type | code | gender | vacId
1 | A | biz | M | 110
2 | A | pro | M | 113
3 | A | tot | F | 114
如果我输入,则根据输入返回vacId,如果代码+类型组合不存在则返回仅等于给定类型的vacId
如何为此编写一个查询,该查询现在可以在pgsql和oracle中使用。
有没有办法可以使用 COALESCE
实现这一目标答案 0 :(得分:1)
您可以使用条件where
子句,例如($
用于参数):
select p.*
from profile p
where case
when exists (
select 1
from profile
where type = $type and code = $code and gender = $gender
)
then type = $type and code = $code and gender = $gender
else type = $type
end;
更新
另一种解决方案可能是在单个json对象中获取结果,这将使您能够使用coalesce()
:
select coalesce(
(select jsonb_agg(to_jsonb(p))
from profile p
where type = $type and code = $code and gender = $gender),
(select jsonb_agg(to_jsonb(p))
from profile p
where type = $type)
);