Selecting a value based on values in different columns

  • I'v a table

    Table1

    With columns

    Col1

    Col2

    Col3

    Col4

    Now i'm populating data from this table to an excel sheet , there is one column 'Type' which can have values 'Col1', 'Col2', 'Col3','Col4'

    depending if its value is 'Y'

    I'm using below code :

    Select

    CASE Col1 WHEN 'Y' THEN 'Col1'

    Else

    CASE Col2 WHEN 'Y' THEN 'Col2'

    Else

    CASE Col3 WHEN 'Y' THEN 'Col3'

    Else

    CASE Col4 WHEN 'Y' THEN 'Col4'

    End As Type

    From table1

    But not sure if its correct , got synatx errors.

  • Hi

    Pls explain clearly.

    "Keep Trying"

  • Perhaps you're trying to do this?

    Select

    CASE

    WHEN Col1 = 'Y' THEN 'Col1'

    WHEN Col2 = 'Y' THEN 'Col2'

    WHEN Col3 = 'Y' THEN 'Col3'

    WHEN Col4 = 'Y' THEN 'Col4'

    END As Type

    FROM table1

    Ryan Randall

    Solutions are easy. Understanding the problem, now, that's the hard part.

  • This is how data will look like :

    Table1:

    Col1 Col2 COl3 Col4

    'Y' '' '' ''

    '' 'Y' '' ''

    '' '' 'Y' ''

    '' '' '' 'Y'

    Data in excel :

    Col Name: Type

    'COl1'

    'Col2'

    'Col3'

    'Col4'

    Hope its clear now...plz let me know , if u need anything else .

  • Thanks Ryaan !!

    It's working .

Viewing 5 posts - 1 through 4 (of 4 total)

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