简体   繁体   中英

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 
   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 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. In your case, it is returning a string value.

The WHERE clause consists of boolean expressions. 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 .)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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