如何对嵌套的case语句求和

时间:2018-02-13 21:16:09

标签: sql oracle11g

我正在尝试创建一个查询,用于计算客户端添加和修改的扣减,以及内部支持代表的扣减。我在第一个" END"之后收到错误ORA-00905错误关键字。在查询中。我无法弄清楚如何对嵌套的case语句求和。

serialize

感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

为什么首先嵌套CASE语句?不应该像

那样
sum(CASE WHEN ded.ACTION = 'A'
        AND REGEXP_LIKE(ded.oper_ID, '[[:digit:]]') THEN 1 ELSE 0 END)

等?

您收到错误是因为CASE ... CASE ...内有两个END但只有一个sum( ... )。除此之外还有一个END,但这实际上只是一个错误的包围组:

(CASE ...) END

答案 1 :(得分:0)

@ Jnevill的帖子帮助我解决了这个问题。我在第一个END之后删除了括号,并在第二个END之后删除了括号,但是在声明别名之前。

SELECT 
sum(CASE WHEN ded.ACTION = 'A' --added deduction
        THEN
        CASE WHEN REGEXP_LIKE(ded.oper_ID, '[[:digit:]]') THEN 1 ELSE 0 END 
END) AS "Added By Clients", -- count # of client deductions

sum(CASE WHEN ded.ACTION = 'A' --added deduction
        THEN
        CASE WHEN NOT REGEXP_LIKE(ded.oper_ID, '[[:digit:]]') THEN 1 ELSE 0 END 
END) AS "Added by Internal Reps", -- count # of client deductions

sum(CASE WHEN ded.ACTION <> 'A' --modified deduction
        THEN
        CASE WHEN REGEXP_LIKE(ded.oper_ID, '[[:digit:]]') THEN 1 ELSE 0 END 
END) AS "Modified By Clients", -- count # of client deductions    

sum(CASE WHEN ded.ACTION <> 'A' -- modified deduction
    THEN
    CASE WHEN NOT REGEXP_LIKE(ded.oper_ID, '[[:digit:]]') THEN 1 ELSE 0 END
END) AS "Modified by Internal Reps" -- count # of internal rep deductions