SQL CASE with more than one condition

1

I wonder if you can use CASE with more than 1 condition.

In this query I have calculations that need to be done when a.operacao == 'C' but that depend on the value of a.DESCRICAO_PREMIO ( '1P' or '1/5P' ) to define which account exactly.

SELECT
  a.*,
  CASE (a.operacao)
    WHEN 'C' THEN  (b.multiplicador *  a.valor_jogo)
  END AS [Valor]
FROM tb_jogo_detalhe a
  INNER JOIN tb_modalidade b ON b.abreviatura = a.operacao
WHERE a.ID = 2222

How to use more than one condition in a CASE ?

    
asked by anonymous 08.05.2016 / 22:13

3 answers

2

Yes, it does. Look:

select  a.*,                                                           
           case
               when a.operacao = 'C' and a.DESCRICAO_PREMIO = '1P' then  (b.multiplicador *  a.VALOR_JOGO)
               when a.operacao = 'C' and a.DESCRICAO_PREMIO = '1/5P'then  (/** coloca o outro calculo aqui e vai indo**/)
           end as valor                                            
           from dbo.TB_JOGO_DETALHE a                                                                               
           inner join tb_modalidade b on b.abreviatura = a.operacao  
           where a.ID = 2222
    
08.05.2016 / 22:32
0

Get it done this way!

           select  a.*,                                                           
           case 
            when (a.operacao = 'C' and a.DESCRICAO_PREMIO = '1P')  then  (b.multiplicador *  a.VALOR_JOGO) 
            when (a.operacao = 'C' and a.DESCRICAO_PREMIO = '1/5P ')  then  ( (b.multiplicador / 5) *  a.VALOR_JOGO)                           
           END AS VALOR_PREMIO 
           from TB_JOGO_DETALHE a                                                                               
           inner join tb_modalidade b on b.abreviatura = a.operacao  
           where a.IDJOGO = 70
    
08.05.2016 / 22:32
0

Yes does follow the basic syntax for using Case

select case when ((condição1) and (condicao2) and (condicao3))
            then 'Verdadeiro'
            else 'Falso'
       end
  from Tabela
    
09.05.2016 / 23:07