简体   繁体   中英

Missing expression error in Select subquery in Case expression

I have to write a SQL with Case expression joining many tables to retrieve 2 fields , which will populate another table B.

Below is some of my case conditions:

  CASE 
     A_FLG = 'Y' and Mod code = 'P' and Year(T_DT) < 2011
     set GRP_ID =  2

Colum TDR_DT is derived from other table C. I compiled my SQL as follows:

   Select 
          case 
             when A.A_FLG = 'Y' and C.MCODE ='P'
             and (
                    select extract(year from C.T_DT) from Table C where
                    extract(year from C.T_DT)
                 )<2010
             then '2'       
             else '-1'                                
   END "GRP_ID",
   from Table A 
   join Table F
   on A.ID=F.ID

On executing the query, I find error "Missing expression" pointing at first Case . I have tried to figure out what could be the error, but am unable to do so. ANy help is deeply appreciated, since I have many more case statements to write and this is holding up my work.

You are not given any relation between the table A , B and C so I am assuming there are no relation. And you want to access the MCODE from the C table in your expression so you can fire up another subquery for that,

  Select 
      case when A.A_FLG = 'Y' 
              and (Select TOP 1 C.MCODE from tablec C Where yourcondition =value) ='P'
              and (select extract(year from C.T_DT) from Table C where extract(year from C.T_DT))<2010
                  then '2'       
           else '-1'                                
      END "GRP_ID",
  from Table A 
       join Table F
        on A.ID=F.ID

I am selecting the Top 1 because you need to make sure the select statement will always returns the one value for each join row. You can remove it if you no for sure that your condition are going to always return single row.

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