Creating a View

  • How can I work around Error 207: invalid Column Name (B) in this example?

    Select A,

    B = Case A

    When 'F' then 'Female'

    Else

    'Male'

    End,

    C = Case B

    When 'Female' Then 'Woman'

    Else

    'Man'

    From test

    This is just an example my real Query is more complicated.

    Thanks in advance.

  • The problem is B does not exist in the data source. You can however do it like so.

    SELECT A, B,

    C = Case B

    When 'Female' Then 'Woman'

    Else

    'Man'

    FROM (Select A,

    B = Case A

    When 'F' then 'Female'

    Else

    'Male'

    End

    From test) AS subTbl

    to get the desired results.

    "Don't roll your eyes at me. I will tape them in place." (Teacher on Boston Public)

  • I don't know if your real query follows the same logic

    but the sample query should work as shown below.

    using the logic

    if

    A = 'F' and

    'F' = 'Female' and

    'Female' = 'Woman'

    Then A = 'Woman'

    if

    A = 'M' and

    'M' = 'Male' and

    'Male' = 'Man'

    Then A = 'Man'

    SELECT

    A,

    Case when A = 'F' Then 'Female' Else 'Male' End as B

    Case when A = 'F' Then 'Woman' Else 'Man' End as C

    From Test

    Good Luck :O)

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply