Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use case in db2 column select query

Tags:

sql

select

db2

I have a table in which there is a varchar column 'someid' and some timestamp columns: 'date_1', ... , date_4 and 'xdate_1', ... , xdate_4 I am trying to select two of them depending on 'someid' value, but had no luck until now. I am sure that this is syntax, googling also didn't help as all the examples were similar to my query.

Heres what I'm trying to do:

select
  case
    when someid = 1 then date_1
    when someid = 2 then date_2
    when someid = 3 then date_3
    when someid = 4 then date_4
 ,case
    when someid = 1 then xdate_1
    when someid = 2 then xdate_2
    when someid = 3 then xdate_3
    when someid = 4 then xdate_4
from mytable;
like image 993
JulioBordeaux Avatar asked Aug 28 '14 10:08

JulioBordeaux


1 Answers

You forgot the end

select
  case
    when someid = 1 then date_1
    when someid = 2 then date_2
    when someid = 3 then date_3
    when someid = 4 then date_4
  end as col1
 ,case
    when someid = 1 then xdate_1
    when someid = 2 then xdate_2
    when someid = 3 then xdate_3
    when someid = 4 then xdate_4
  end as col2
from mytable;
like image 61
juergen d Avatar answered Oct 08 '22 09:10

juergen d