CASE statement if else Magic ETL

Hey guys I am trying to fill a column based on certain criteria from 4 columns.

I think it would be best to do this in another column so that I'm not overwriting anything. 

My table looks like the following and I'm trying a Case Statement that fills in the null spaces, but I think my case statement is missing something or I'm missing a step. 

publish_datedate_createdtime_createddate_updatedtime_updated
04/30/202004/30/202018:37:0604/30/202018:37:06
04/30/202004/30/202018:38:0604/30/202018:38:06
 04/30/202017:09:2904/30/202017:09:29
04/30/202004/30/202018:37:0804/30/202018:43:11
 04/30/202019:06:0704/30/202019:06:07
04/30/202004/30/202018:37:0804/30/202018:43:11
 04/30/202012:00:3304/30/202012:00:36
04/30/202004/30/202018:38:0504/30/202022:31:32

 

My case statement looks like this so far:

CASE when `date_created`=`date_updated` and `time_created`=`time_updated` and `publish_date` IS NULL then `date_created` else `publish_date` end

Unfortunately my case statement sucks and this doesn't work to my advantage. 

 

Can anyone help?

Comments

  • GrantSmith
    GrantSmith Indiana 🔴

    Hi @user052846 

    You didn't clearly state what exactly your issue was with your beast mode so I'm somewhat guessing here but you're essentially wanting to use the most recent date between publish date and update date  for the publish date if the publish date is null correct?

     

    If that's correct then you have two independent conditions dependent upon each other. Essentially you're saying the dates must be the same and publish date must be null in order for you to use the creation date.  I'm assuming here too that your creation date and updated date will be the same when an entry is created. With those assumptions you can just narrow down your beast mode to using the updated date if the publish date is null.

     

    Simplifies down to:

    IFNULL(`publish_date`, `date_updated`)

     

     

  • jaeW_at_Onyx
    jaeW_at_Onyx Budapest / Portland, OR 🟤

    a series of nested IFNULL statements will get the job done as per Grant's recommendation, but have a look at COALESCE() it will return the first non null value in a chain.

     

    if you want to write it as a case statement then do a series of null checks

     

    case 

    when colA is not null then colA

    when colB is not null then colB

    when colC is not null then colC

    ...

    else ...

    end

     

    by definition you don't need to build extensive logic b/c in order for the second clause (test for B) to apply, colA must have been null (otherwise the preceeding logic step would have applied).