Como usar o caso na coluna db2 seleccione a pesquisa
tenho uma tabela na qual há uma coluna de varchar 'someid'
e algumas colunas de datas:
'date_1', ... , date_4
e 'xdate_1', ... , xdate_4
Estou tentando selecionar dois deles dependendo do valor 'someid' , mas não tive sorte até agora. Estou certo de que esta é a sintaxe, o Google também não ajudou, pois todos os exemplos foram semelhantes à minha consulta.
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;
2
Author: JulioBordeaux, 2014-08-28
2 answers
Esqueceste-te do ...
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;
11
Author: juergen d, 2014-08-28 10:31:06
Como nota, se someid
é uma coluna de caracteres, então deve usar comparações de caracteres:
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),
. . .
from mytable;
0
Author: Gordon Linoff, 2014-08-28 10:55:01