自定义列上的 Where 子句
Where clause on custom column
select c.base_id,case when c.type is not null then c.type else g.type end as type, c.function_id
from cust c
left join category cg on c.cat_id=cg.id
where c.type='privelleged'; --- where clause is not working as expected
上面的 query.The where 子句没有按预期工作,我漏掉了什么。
我需要在派生的 'type' 列上应用 where 子句。我如何在 Oracle 中实现它。
提前致谢
我怀疑您只想 coalesce()
-- 在 select
和 where
中:
select c.base_id, coalesce(c.type, g.type) as type, c.function_id
from cust c left join
category cg
on c.cat_id = cg.id
where coalesce(c.type, g.type) = 'privileged';
您不能按照您想要的方式使用 derived 列 - 您还需要在 WHERE 中使用相同的 CASE:
select c.base_id,
case when c.type is not null then c.type
else g.type
end as type,
c.function_id
from cust c
left join category cg on c.cat_id = cg.id
where case when c.type is not null then c.type --> here
else g.type
end = 'privelleged';
或者,将当前查询用作 CTE(或子查询)并对其应用过滤器:
with temp as
(select c.base_id,
case when c.type is not null then c.type
else g.type
end as type,
c.function_id
from cust c
left join category cg on c.cat_id = cg.id
)
select *
from temp
where type = 'privelleged';
select c.base_id,case when c.type is not null then c.type else g.type end as type, c.function_id
from cust c
left join category cg on c.cat_id=cg.id
where c.type='privelleged'; --- where clause is not working as expected
上面的 query.The where 子句没有按预期工作,我漏掉了什么。 我需要在派生的 'type' 列上应用 where 子句。我如何在 Oracle 中实现它。
提前致谢
我怀疑您只想 coalesce()
-- 在 select
和 where
中:
select c.base_id, coalesce(c.type, g.type) as type, c.function_id
from cust c left join
category cg
on c.cat_id = cg.id
where coalesce(c.type, g.type) = 'privileged';
您不能按照您想要的方式使用 derived 列 - 您还需要在 WHERE 中使用相同的 CASE:
select c.base_id,
case when c.type is not null then c.type
else g.type
end as type,
c.function_id
from cust c
left join category cg on c.cat_id = cg.id
where case when c.type is not null then c.type --> here
else g.type
end = 'privelleged';
或者,将当前查询用作 CTE(或子查询)并对其应用过滤器:
with temp as
(select c.base_id,
case when c.type is not null then c.type
else g.type
end as type,
c.function_id
from cust c
left join category cg on c.cat_id = cg.id
)
select *
from temp
where type = 'privelleged';