Advent of Code is back! Unwrap daily challenges to sharpen your Alteryx skills and earn badges along the way! Learn more now.
Free Trial

Alteryx Designer Desktop Discussions

Find answers, ask questions, and share expertise about Alteryx Designer Desktop and Intelligence Suite.
SOLVED

In-DB Nested IF Statement

KW_22
5 - Atom

How do you create a nested IF/THEN statement while working with In-DB tools? 

 

For example, I want to use the Formula In-DB tool to create a field called "Age Segment".  The statement should look at my Age field and determine if it falls into these Age Segments:

 

Not provided, 18 and Under, 19 to 64, 65 or Older

 

I have tried both of the following and both returned error messages. 

 

CASE WHEN "Age" = -1 THEN 'Not Provided' ELSE CASE WHEN "Age" < 19 THEN '18 and Under' ELSE CASE WHEN "Age" between 19 and 64 THEN '19 to 64' ELSE CASE WHEN "Age" >= 65 THEN '65 Or Older' else 'Null' end 

 

IF "Age" = -1 THEN 'Not Provided' ELSE IF "Age" < 19 THEN '18 and Under' ELSE IF "Age" between 19 and 64 THEN '19 to 64' ELSE IF "Age" >= 65 THEN '65 Or Older' else 'Null' END IF  

 

Any tips or tricks would be appreciated! Thanks!

2 REPLIES 2
MSalvage
11 - Bolide

@KW_22,

 

Let me start by saying I am not totally sure whether it matters or not what DB you are using. I can say that if you are using a SQL Server connection then the following should work.

 

CASE
    WHEN
    "Age" = -1
        THEN
            'Not Provided'
    WHEN
    "Age" < 19
        THEN
            '18 and Under'
WHEN
    "Age" < 65
        THEN
            '19 to 64'

ELSE

           '65+'

END

 

Hope this helps,

 

MSalvage

KW_22
5 - Atom

Yes, that statement worked! Thank you so much :) 

Labels
Top Solution Authors