select中不能用别的列的别名吗

tanshihou 2013-11-18 11:00:53
我想写一个select,其中一个字段是
case aaa
when 1 then a * b / 100
when 2 then a / (100 + b) * b
end

而其中的b是一个有点复杂的子查询,

我想写成
select
(select ccc from ddd where ......) as b,
case aaa
when 1 then a * b / 100
when 2 then a / (100 + b) * b
end
from ...

可是好像编译不过去,我必须写成下面这样吗

select
(select ccc from ddd where ......) as b,
case aaa
when 1 then a * (select ccc from ddd where ......) / 100
when 2 then a / (100 + (select ccc from ddd where ......) ) * (select ccc from ddd where ......)
end
from ...


谢谢了
...全文
237 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
發糞塗牆 2013-11-18
  • 打赏
  • 举报
回复
如果你要单层调用,需要把产生别名的那个列用上,因为SELECT 里面,所有列都是“同时”产生,不是说左边先生成再生成后边的列。或者你在外层再包一层
LongRui888 2013-11-18
  • 打赏
  • 举报
回复
简单的方法就是,在外面再套一层,这样: select b, case aaa when 1 then a * b / 100 when 2 then a / (100 + b) * b end from ( select (select ccc from ddd where ......) as b, ... from ... )t 这样就简单多了哈
tcmakebest 2013-11-18
  • 打赏
  • 举报
回复
select ... from ... join (select ccc from ddd where ......) as b on ...
_小黑_ 2013-11-18
  • 打赏
  • 举报
回复
在套 一层 select

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧