日期:2014-05-17  浏览次数:20785 次

Oracle 使用别名进行比较
select w2_month as x,w2_week as y,x-y as z from table

例子:这里我用了x  y  2个别名分别表示2个字段(number类型)  现在我想用别名进行相减  这种好像不可以,请问有什么办法可以解决吗?


实际需求:W2实际入库周 是更加一个select 语句获取的

  --case when(W2实际入库周 - W2计划入库周) > 0 then '10'
            --        when(W2实际入库周- W2计划入库周) = 0 then '20'
              --       when(W2实际入库周 -W2计划入库周) < 0 then '30'
           --end as 'W2是否按期入库类型',

------解决方案--------------------
报什么错啊?
------解决方案--------------------
select 中-nvl(国,0) from ( select sal as 中 ,comm as 国 from emp) t;


测试了下,好像没问题哦
------解决方案--------------------
直接用原来列名不就得了,非要用别名?
------解决方案--------------------
select w2_month as x,w2_week as y,x-y as z from table
这个会被当做单独列来处理

不知道这样子和需求相符合吗?

hr@ORCL> create table t1 (id1 number,id2 number);

Table created.

hr@ORCL> insert into t1 values(1,2);

1 row created.

hr@ORCL> commit;

Commit complete.

hr@ORCL> with a as (select id1 x,id2 y,id1-id2 as z from t1)
  2      select x,y,z from a;

         X          Y          Z
---------- ---------- ----------
         1          2         -1

------解决方案--------------------
在外面嵌套一层就可以了 
------解决方案--------------------
哪个数据库也不支持这么写,同一层的你能直接用别名引用?

想最简单实现,就写成

select x,y,x-y z from 
  (select w2_month x ,w2_week y  from table)
------解决方案--------------------
引用:
哪个数据库也不支持这么写,同一层的你能直接用别名引用?

想最简单实现,就写成

select x,y,x-y z from 
  (select w2_month x ,w2_week y  from table)


按照楼上说的做比较妥~!
------解决方案--------------------
支持12楼,用子查询