能在where中用case嗎?????

billlyh 2011-04-11 04:54:41
能在where中用case嗎?????
現在數據表xwp_out_quantity 的數據如下:
vendor item
220010 A90215R
220010 A90200R

220020 A80216R
220020 A80201R

220030 A70218R
220030 A70204R

想把這表的數據分拆:
當vendor=220010 時,把item第二位為9的都顯示出來;
當vendor=220020 時,把item第二位為8的都顯示出來;
當vendor=220030 時,把item第二位為7的都顯示出來;


select
substr(xoq.item,2,5) model
from xwp_out_quantity xoq
where xoq.vendor='&s_vendor'
and
( case
when xoq.vendor='220010' then substr(xoq.item,2,1) in (9)
when xoq.vendor='220020' then substr(xoq.item,2,1) in (8)
when xoq.vendor='220030' then substr(xoq.item,2,1) in (7)

運行後,PLSQL Developer報錯:
mission keyword

哪位幫幫啊????????????
...全文
397 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaozhang8383 2011-04-11
  • 打赏
  • 举报
回复
没这么用过 干嘛非得这么用?
Well 2011-04-11
  • 打赏
  • 举报
回复
YES...
fly2sky 2011-04-11
  • 打赏
  • 举报
回复
decode 就ok了 虽然看起来也不怎么省事

select
substr(xoq.item,2,5) model
from xwp_out_quantity xoq
where xoq.vendor='&s_vendor'
and substr(xoq.item,2,1) = decode( xoq.vendor,'220010','9','220020','8','220030','7')
luoyoumou 2011-04-11
  • 打赏
  • 举报
回复
-- 这里不需要用 case when语句,用case when语句只会使你的查询更慢,
-- 从你写的这个SQL语句,可以看出:你的SQL功底不是一般的差!
luoyoumou 2011-04-11
  • 打赏
  • 举报
回复
select 
substr(xoq.item,2,5) model
from xwp_out_quantity xoq
where xoq.vendor='&s_vendor'
and ( ( xoq.vendor='220010' and substr(xoq.item,2,1)='7' )
or ( xoq.vendor='220020' and substr(xoq.item,2,1)='8' )
or ( xoq.vendor='220030' and substr(xoq.item,2,1)='9' )
);

-- 注意数值打上引号,用等于(=),别用 in(7) 的格式!
  • 打赏
  • 举报
回复
select  
substr(xoq.item,2,5) model
from xwp_out_quantity xoq
where xoq.vendor='&s_vendor'
and (case when xoq.vendor='220010' then substr(xoq.item,2,1) else null end)=9
or (case when xoq.vendor='220020' then substr(xoq.item,2,1) else null end)=8
or (case when xoq.vendor='220030' then substr(xoq.item,2,1) else null end)=7

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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