Nested Case and Concat Statement

Options

Hi,

I am trying to figure out how to create an Etl that expresses: If a particular year has a result then concat that with another column's result. Something like:

CONCAT(case WHEN 2015= 1 then '2015', '-', Virtual vs In-Person else '' end)

2015 has a 1/0 constant and I want the result to be if they attended in person (so if 2015 = 1 then the result is 2015-Virtual or 2015-In-Person depending on what is in the Virtual vs In-Person Column)

Thanks in advance!

Tagged:

Best Answer

  • GrantSmith
    GrantSmith Coach
    edited March 17 Answer ✓
    Options

    Something like this should work for you:

    CASE WHEN `2015`=1 THEN CONCAT('2015-', `Virtual vs In-Person`) ELSE '' END
    

    **Was this post helpful? Click Agree or Like below**
    **Did this solve your problem? Accept it as a solution!**

Answers

  • GrantSmith
    GrantSmith Coach
    edited March 17 Answer ✓
    Options

    Something like this should work for you:

    CASE WHEN `2015`=1 THEN CONCAT('2015-', `Virtual vs In-Person`) ELSE '' END
    

    **Was this post helpful? Click Agree or Like below**
    **Did this solve your problem? Accept it as a solution!**
  • Cfunk
    Cfunk Member
    Options

    Thank you so much! I don't know why I thought I needed the Concat to envelop the whole thing. :)

  • Cfunk
    Cfunk Member
    Options

    Another question @grantsmith. How do I nest it with multiple years? I tried using:

    CASE WHEN `2015`=1 THEN CONCAT('2015-'

    `Virtual vs In-Person`) ELSE '' END

    CASE WHEN `2016`=1 THEN CONCAT('2016-'

    `Virtual vs In-Person`) ELSE '' END

    CASE WHEN `2017`=1 THEN CONCAT('2017-'

    `Virtual vs In-Person`) ELSE '' END

    CASE WHEN `2018`=1 THEN CONCAT('2018-'

    `Virtual vs In-Person`) ELSE '' END

    CASE WHEN `2019`=1 THEN CONCAT('2019-'

    `Virtual vs In-Person`) ELSE '' END

    CASE WHEN `2020`=1 THEN CONCAT('2020-'

    `Virtual vs In-Person`) ELSE '' END

    CASE WHEN `2021`=1 THEN CONCAT('2021-'

    `Virtual vs In-Person`) ELSE '' END

    CASE WHEN `2022`=1 THEN CONCAT('2022-'

    `Virtual vs In-Person`) ELSE '' END

    CASE WHEN `2023`=1 THEN CONCAT('2023-'

    `Virtual vs In-Person`) ELSE '' END

    CASE WHEN `2024`=1 THEN CONCAT('2024-'

    `Virtual vs In-Person`) ELSE '' END

    CASE WHEN `2025`=1 THEN CONCAT('2025-'

    `Virtual vs In-Person`) ELSE '' END

    But I keep getting an unknown error message in the ETL.

  • GrantSmith
    Options

    you only need the first CASE statement all the others should just be WHEN and only a single else at the end and one END at the very end

    **Was this post helpful? Click Agree or Like below**
    **Did this solve your problem? Accept it as a solution!**
  • GrantSmith
    Options

    Alternatively I’d look into unpivoting your data so it’s structured with a year column and the actual year value for each row then you don’t need the massive case statement to update every year and it’d be a simple CONCAT function with the year column and your virtual in person column

    **Was this post helpful? Click Agree or Like below**
    **Did this solve your problem? Accept it as a solution!**
  • Cfunk
    Cfunk Member
    Options

    I got this error when I tried it like this. I don't think I can use unpivot. I had to pivot and add the constant that I needed to get to this point.

    Thanks for your help!!!

  • marcel_luthi
    marcel_luthi Coach
    edited March 18
    Options

    You have a comma at the end of the first line, you don't separate WHEN statements with commas, this is most likely why you're getting the error with your syntax. I do agree with @GrantSmith that this could be a good use case for unpivoting, specially if a single Row might have a value of 1 for more than a single year, as the beast mode will only return a value for the first year that matches.

  • Cfunk
    Cfunk Member
    Options

    Thank you so much!!