select demvbtn,
deomsch,
(select count(*)
from drmvctl a, drmvtbg b
where a.deblgty = b.demvbtn
and a.deblgty = :demvbtn) as counter,
case
when counter > 0
then 'u'
else ''
end ctrl
from drmvtbg
where demvbtn = :demvbtn
答案 0 :(得分:2)
counter
别名不可用于同一级别的select
子句,因为在评估该选择时,尚未计算counter
。这里的一种解决方案是使用CTE,该CTE首先计算counter
别名:
with cte as (
select demvbtn, deomsch,
(select count(*) from drmvctl a inner join drmvtbg b
on a.deblgty = b.demvbtn where a.deblgty = :demvbtn) as counter
from drmvtbg
where demvbtn = :demvbtn
)
select demvbtn, deomsch, counter,
case when counter > 0 then 'u' else '' end ctrl
from cte;
请注意,我还删除了带有现代显式内部联接的旧式逗号联接。这是近来编写联接的首选方式。
答案 1 :(得分:1)
使用子查询作为列别名在同一选择中不起作用
select *,
case
when counter > 0
then 'u'
else ''
end ctrl from
(
select demvbtn,
deomsch,
(select count(*)
from drmvctl a join drmvtbg b
on a.deblgty = b.demvbtn
where a.deblgty = :demvbtn) as counter
from drmvtbg
where demvbtn = :demvbtn
)A
答案 2 :(得分:1)
内联别名在oracle use子查询中不支持
select emvbtn,
deomsch,case
when counter > 0
then 'u'
else ''
end ctrl
from (select demvbtn,
deomsch,
(select count(*)
from drmvctl a join drmvtbg b
on a.deblgty = b.demvbtn
where a.deblgty = :demvbtn) as counter
from drmvtbg
where demvbtn = :demvbtn
) t
答案 3 :(得分:1)
我想你想要
select demvbtn, deomsch, counter,
(case when counter > 0 then 'u'
end) as ctrl
from (select dt.demvbtn, dt.deomsch,
(select count(*)
from drmvctl dc
where dc.deblgty = dt.demvbtn and
dc.deblgty = dt.demvbtn
) as counter
from drmvtbg dt
where dt.demvbtn = :demvbtn
) d;
注意:
FROM
子句中使用逗号。JOIN
。else
表达式中不需要case
。