Results 1 to 3 of 3

Thread: Case Statement

  1. #1
    Join Date
    Feb 2013
    Posts
    18

    Unanswered: Case Statement

    Hi All,

    Im sure this is very simple
    I have a case statement that i am trying to use in order to add another column to my table. The Case statement checks to see if two conditions are true and if they are it adds 1 to a new column.

    Column A contains an event. Column B contains freetext of varying descriptions in this case Test 1 Test 2 etc

    The Statement is as follows

    Code:
    SELECT 	AL1.A, 
    		AL1.B, 
    		AL1.C, 
    		AL1.D, 
    		AL1.E, 
    		AL1.F, 
    		AL1.G, 
    		AL1.H, 
    		AL1.I, 
    		AL1.J, 
    		AL1.K, 
    		AL1.L, 
    		AL1.M, 
    		AL1.N, 
    		AL1.O,		
    		CASE	WHEN AL1.A = 'XX' AND AL1.function IN('Test 1','Test 2','Test 3') THEN 1 ELSE 0
    			WHEN AL1.event_cd = 'AD' AND AL1.function IN('Test 1') THEN 1 ELSE 0 END   
    									
    FROM 			public."Sample_Table" AL1

    The error message is as follows

    ERROR: syntax error at or near "WHEN"
    LINE 17:

    ********** Error **********

    ERROR: syntax error at or near "WHEN"
    SQL state: 42601
    Character: 410





    Any help would be greatly appreciated

    Thanks

  2. #2
    Join Date
    Dec 2012
    Location
    Logan, Utah
    Posts
    163
    Quote Originally Posted by maccten View Post
    Hi All,

    CASE WHEN AL1.A = 'XX' AND AL1.function IN('Test 1','Test 2','Test 3') THEN 1 ELSE 0
    WHEN AL1.event_cd = 'AD' AND AL1.function IN('Test 1') THEN 1 ELSE 0 END
    Try losing the ELSE 0 after the first WHEN
    Code:
    CASE	WHEN AL1.A = 'XX' AND AL1.function IN('Test 1','Test 2','Test 3') THEN 1
    	WHEN AL1.event_cd = 'AD' AND AL1.function IN('Test 1') THEN 1 ELSE 0 END

  3. #3
    Join Date
    Feb 2013
    Posts
    18
    Thank you, this appears to be the answer

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •