Search code examples
oracle-databasealiasoracle12c

How do I add alias based on values of table


As my title, for ex I have a table A and it has values from 1 to 10.

I want to Select value 1 and 2 as "First" column name, 3 and 4 as "Second" column name v.v.

Look like this:

|First| |Second|
   1       3
   2       4
   1       4

Thanks!


Solution

  • Using CASE, perhaps?

    SQL> with test as
      2    (select level val from dual
      3     connect by level <= 5
      4    )
      5  select case when val <= 2 then val end first,
      6         case when val  > 2 then val end second
      7  from test;
    
         FIRST     SECOND
    ---------- ----------
             1
             2
                        3
                        4
                        5
    
    SQL>
    

    It would help, though, if you provided sample data and explained what to do with values that aren't contained in (1, 2, 3, 4).