SQL CASE with more than one condition

Asked

Viewed 15,705 times

1

I would like to know if it is possible to use the CASE more than 1 condition.

In this query I have calculations that need to be done when a.operacao == 'C' but which 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?

3 answers

2

There is. Look at that:

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

  • I really appreciate your help! thanks!

0

Has yes follow basic syntax to use Case

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

0

Can do it 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.