简体   繁体   English

缺少表达式sql

[英]missing expression sql

SELECT
  create_date
  ,resolved_date
  ,item
  ,site
  ,status
  ,contact_time
  ,impact_label
FROM
  mytable  
WHERE
  create_date BETWEEN to_date('2013/03/01','YYYY/MM/DD') 
   AND to_date('2015/08/06','YYYY/MM/DD')
and CASE item 
  WHEN in ('A','B') then '1'
  WHEN in ('C') then '2'
  WHEN in ('D') then '3'
  ELSE null 
  END 
GROUP BY
create_date
,resolved_date
,item
,site
,status
,contact_time
,impact_label

It says I have problem at the first when in, could someone please help? 它说我第一次进屋时遇到问题,有人可以帮忙吗?

If you want to use IN inside CASE you need different syntax : 如果要在CASE使用IN ,则需要不同的语法:

...
case 
   when item  in ('A','B') then '1' 
   when item in  ('C') then '2' 
   when item  in ('D') then '3' 
   --else null   -- no need , ELSE NULL is default behaviour of CASE
end ...

Or change CASE to correct simple search syntax: 或更改CASE来更正简单的搜索语法:

 CASE item 
  WHEN 'A' THEN '1'
  WHEN 'B' THEN '1'
  WHEN 'C' THEN '2'
  WHEN 'D' THEN '3'
  ELSE NULL 
 END 

CASE is an expression that returns a value. CASE是一个返回值的表达式。 In your case, it is returning a string value. 在您的情况下,它将返回一个字符串值。

The WHERE clause consists of boolean expressions. WHERE子句由布尔表达式组成。 A string is not a boolean expression. 字符串不是布尔表达式。 I'm not sure what you intend, perhaps: 我不确定您打算做什么,也许:

(CASE WHEN item in ('A','B') then '1'
      WHEN item in ('C') then '2'
      WHEN item in ('D') then '3'
      ELSE null 
 END) IS NOT NULL

(Note the changes both to the CASE and the addition of IS NOT NULL .) (请注意对CASE的更改以及对IS NOT NULL 。)

However, that is a silly expression, because it is equivalent to: 但是,这是一个愚蠢的表达,因为它等效于:

item in ('A', 'B', 'C', 'D')

Perhaps you want to add the CASE to the SELECT , giving it a proper column alias. 也许您想将CASE添加到SELECT ,为其提供适当的列别名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM