Solved

Oracle SQL:  getting syntax error on sql ... snippet below...

Posted on 2013-06-25
6
448 Views
Last Modified: 2013-06-25
Hi EE,

can you pls help with deciperhing the syntax error below...

am creating a complicated sql...

  it's bec. a student's attribute can reside in either 2 fields

       student_attribute_1
       student_attribute_2

 so am creating a field called calc_attribute to hold the attribute

bec want to create an excel file, to run a Microsoft word mail merge
so want the attribute in one place

i do this using a case when statement....

====

now if the calc_attrib field = MDMP  or MDPH then it's a dual degree medical student student
    and i want to create new fields:
           MD_Matric_Month,
           MD_ Matric_Year
           MD_ADG

i need to derive MD_Matric_Month  from the field: ACADEMIC_PERIOD_MATRICULATED
       and calculate as a named month

term:  201310    10 =  June
            201320   20=   August
            201330   30 =  January

so i am having trouble with the case when statement that are assigning these month values

the case where it's trying to calculate 'August' is giving an error:

"FROM KEYWORD NOT FOUND WHERE EXPECTED"

tx so much for your help, sandra

below is a code snippet:

SELECT  
        ACADEMIC_PERIOD
        ,COLLEGE
        ,PROGRAM          
        ,ID
        ,NAME
        ,STUDENT_ATTRIBUTE_1
        ,STUDENT_ATTRIBUTE_2        
         ,ACADEMIC_PERIOD_MATRICULATED
        ,CASE WHEN   STUDENT_ATTRIBUTE_1 = 'SD' OR STUDENT_ATTRIBUTE_2 = 'SD'  THEN 'SD'
             WHEN   STUDENT_ATTRIBUTE_1 = 'TR' OR STUDENT_ATTRIBUTE_2 = 'TR'  THEN 'TR'
             WHEN   STUDENT_ATTRIBUTE_1 = 'MDPH' OR STUDENT_ATTRIBUTE_2 = 'MDPH'   THEN 'MDPH'
             WHEN   STUDENT_ATTRIBUTE_1 = 'MDMP' OR STUDENT_ATTRIBUTE_2 = 'MDMP'   THEN 'MDMP' END
           AS Calc_Attrib

     
       ,CASE WHEN (CASE              
                        WHEN   STUDENT_ATTRIBUTE_1 = 'MDPH' OR STUDENT_ATTRIBUTE_2 = 'MDPH'   THEN 'MDPH'
                        WHEN   STUDENT_ATTRIBUTE_1 = 'MDMP' OR STUDENT_ATTRIBUTE_2 = 'MDMP'   THEN 'MDMP' END) IN ('MDMP','MDPH')
                                AND SUBSTR(Academic_Period_Matriculated,5,2) = '10' THEN 'June' END    
                               
             WHEN (CASE                            
                        WHEN   STUDENT_ATTRIBUTE_1 = 'MDPH' OR STUDENT_ATTRIBUTE_2 = 'MDPH'   THEN 'MDPH'
                        WHEN   STUDENT_ATTRIBUTE_1 = 'MDMP' OR STUDENT_ATTRIBUTE_2 = 'MDMP'   THEN 'MDMP' END) IN ('MDMP','MDPH')
                                AND SUBSTR(Academic_Period_Matriculated,5,2) = '20' THEN 'August' END                                
                               
                               
           as Calc_DualMatricMonth
0
Comment
Question by:mytfein
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 32

Assisted Solution

by:awking00
awking00 earned 300 total points
ID: 39275160
You have two case statements within a case statement. Each of the inner case statements include an END keyword, but you also have two END keywords for the single outer case statement.
0
 
LVL 77

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 200 total points
ID: 39275166
>>THEN 'June' END

This END kills the outer CASE.
0
 
LVL 32

Assisted Solution

by:awking00
awking00 earned 300 total points
ID: 39275180
...
'MDMP'   THEN 'MDMP' END) IN ('MDMP','MDPH') AND SUBSTR(Academic_Period_Matriculated,5,2) = '10' THEN 'June' END => NOT NEEDED
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 32

Assisted Solution

by:awking00
awking00 earned 300 total points
ID: 39275187
Beat me again :-)
0
 
LVL 77

Accepted Solution

by:
slightwv (䄆 Netminder) earned 200 total points
ID: 39275192
When nesting code like this, I always like to line things up.  It's easier to locate mistakes that way:

SELECT  
       CASE WHEN (CASE              
                        WHEN   STUDENT_ATTRIBUTE_1 = 'MDPH' OR STUDENT_ATTRIBUTE_2 = 'MDPH'  
                           THEN 'MDPH'
                        WHEN   STUDENT_ATTRIBUTE_1 = 'MDMP' OR STUDENT_ATTRIBUTE_2 = 'MDMP'
                           THEN 'MDMP'
                  END
                 ) IN ('MDMP','MDPH')
                 AND SUBSTR(Academic_Period_Matriculated,5,2) = '10'
       THEN 'June'
      END
             WHEN (CASE                            
                        WHEN   STUDENT_ATTRIBUTE_1 = 'MDPH' OR STUDENT_ATTRIBUTE_2 = 'MDPH'  
                            THEN 'MDPH'
                        WHEN   STUDENT_ATTRIBUTE_1 = 'MDMP' OR STUDENT_ATTRIBUTE_2 = 'MDMP'  
                            THEN 'MDMP' END
                  ) IN ('MDMP','MDPH')
                  AND SUBSTR(Academic_Period_Matriculated,5,2) = '20'
       THEN 'August'
       END                                
       as Calc_DualMatricMonth
0
 

Author Comment

by:mytfein
ID: 39275341
tx so much everyone,

removed the 'END'

it works now...

tx again, s
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question