SQL语句case when

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL语句case when相关的知识,希望对你有一定的参考价值。

select trunc(date '2009-01-01','dd') as date_id, b.org_id as org_id, b.risk_id as risk_id, sum(a.planfee-nvl(c.payreffee,0)) , (case when to_char(statistic_date-payrefdate>=0) and to_char(statistic_date-payrefdate<90) then sum(a.planfee-nvl(c.payreffee,0)) else '' end), (case when to_char(statistic_date-payrefdate>=90) and to_char(statistic_date-payrefdate<180) then sum(a.planfee-nvl(c.payreffee,0)) else '' end), (case when to_char(statistic_date-payrefdate>=180) and to_char(statistic_date-payrefdate<360) then sum(a.planfee-nvl(c.payreffee,0)) else '' end), (case when to_char(statistic_date-payrefdate>=360) and to_char(statistic_date-payrefdate<720) then sum(a.planfee-nvl(c.payreffee,0)) else '' end), (case when to_char(statistic_date-payrefdate>=720) then sum(a.planfee-nvl(c.payreffee,0)) else '' end) from dm_f_prpjplanfee_prop a,dm_policy_prop_cnt b,(select certino,serialno,sum(payreffee) as payreffee from dm_f_prpjpayrefrec_prop where payrefdate<=(date'2009-01-31') group by certino,serialno) cwhere a.certino=b.endorse_no and a.certino=c.certino(+) and a.serialno=c.serialno(+)and not exists (select 1 from dm_policy_prop_cnt where b.policy_no = policy_no and endorse_type_code = '19' and certi_type = 'E' and statistic_date <= date '2009-01-31') --and b.policy_no='605012008110000000741' and b.statistic_date <= date '2009-01-31' group by trunc(date '2009-01-31','dd'),b.org_id,b.risk_id 这是一个SQL里面的case when语句,如果执行这个语句,会提示缺少右括号,但如果把case when语句去掉,就可以执行这个语句。有没有谁能指点下啊?

参考技术A 太长了,但是看到一个问题,case
when,要求返回的类型必须一直。
你看你的第一个case
when语句
(case
when
to_char(statistic_date-payrefdate>=0)
and
to_char(statistic_date-payrefdate<90)
then
sum(a.planfee-nvl(c.payreffee,0))
else
''
end)
then后边返回的是一个求和,那么类型肯定是数字,而else分支,其他情况下居然返回了空字符串,字符型。这两个不一致,首先就错了。你先改了这些吧。

SQL中case when then用法

sql语句判断方式之一Case。具有两种格式:简单的Case函数、Case搜索函数。

1、简单Case函数

  CASE sex

  WHEN ‘1‘ THEN ‘男‘

  WHEN ‘2‘ THEN ‘女‘

  ELSE ‘其他‘ 

  END

2、Case搜索函数

  CASE 

  WHEN sex=‘1‘ THEN ‘男‘

  WHEN sex=‘2‘ THEN ‘女‘

  ELSE sex=‘其他‘ 

  END

以上是关于SQL语句case when的主要内容,如果未能解决你的问题,请参考以下文章

SQL语句case怎么判断这个字段为空

case when用法sql

SQL case语句

sql 的case when 语句

SQL语句中Case 的用法

SQL语句case怎么判断这个字段为空