比较解码的结果

时间:2015-07-15 18:43:59

标签: sql oracle

如何比较我从解码函数得到的结果与另一个值。这是查询。

select  employee,
SUM(DECODE(type, 'credit' , amount,
                  'debit', allAmount)) result, 
sign(result > 1000)
from table group by employee;

我使用此SUM(DECODE(type, 'credit' , amount, 'debit', allAmount))结果获得的结果必须与名为' 1000'的某些值进行比较。怎么比较?

2 个答案:

答案 0 :(得分:2)

with x as 
(select  employee,
SUM(DECODE(type, 'credit' , amount,
                 'debit', allAmount)) result
from table group by employee)
select * from x where result > 1000

假设您需要符合result > 1000条件的行,这将完成工作。

答案 1 :(得分:2)

另一种方法:

select employee
     , sum(decode(type, 'credit', amount
                      , 'debit', allAmmount)) result
  from table
 group by employee
having sum(decode(type, 'credit', amount
                      , 'debit', allAmmount)) > 1000;