Use IF function in SQL as if it were excel

Asked

Viewed 2,342 times

1

Exists in SQL a function equivalent to IF (SE) excel? I need to do a logical test which in excel I would write like this =SE(B1_UM = "UN"; D2_QUANT / B5_QE1;D2_QUANT).

Here is my query:

SELECT
   D2_FILIAL,
   // Um monte de coluna...
   CASE
      B1_UM 
      WHEN
         'UN' 
      THEN
         D2_QUANT / B5_QE1 
      ELSE
         D2_QUANT 
   END AS CALC_QUANT 
FROM
   SD2010 SD2 
   // Vários joins
WHERE com várias condições
GROUP BY
   D2_FILIAL, 
   // Várias outras colunas no group
   CALC_QUANT
  • And what the query that you are currently using?

  • What exactly is the condition you want to test and the outcome you are waiting for?

  • @Leandroangelo I’m wanting the query to make a calculation based on a logic test, I talked to a friend and I arrived at this result CASE B1_UM WHEN 'UN' THEN D2_QUANT / B5_QE1 ELSE D2_QUANT END AS CALC_QUANT apparently it should work but DB is not recognizing the alias we created is returning the error Error : 904 - ORA-00904: "CALC_QUANT": invalid identifier

  • Mhac you seem to be using Oracle right ? You can do it like this... select case name_column when name_column = 1 then 'Show what you want' when name_column = 2 then 'show .... ' Else then 'xxxxx' end as 'XYZ' ' ....

  • @Lucasbrogni oracle11g

  • 1
  • Look at this response from Maniero on this subject. Although the question is about a different bank, the theory explained here is the same.

Show 2 more comments

2 answers

0

To treat conditional results in you have basically three structures:

1. IIF(boleana expression, value if true, false value)

Example:

SELECT IIF(id = 1, 'VERDADEIRO', 'FALSO') as Expressao
FROM tabela

2. CASE expression WHEN value 1 THEN score 1 (...) END

Example:

SELECT CASE id 
           WHEN 1 THEN 'ID é 1'
           WHEN 2 THEN 'ID é 2'
           ELSE 'ID é outra coisa'
       END as Expressao
FROM tabela

3. CASE WHEN boleana expression THEN outworking (...) END

Example:

SELECT CASE  
           WHEN id = 1 THEN 'ID é 1'
           WHEN id > 5 THEN 'ID é maior que cinco'
           ELSE 'Não sei o que fazer com o ID'
       END as Expressao
FROM tabela

I hope this has cleared up your doubts.

  • Thank you Diego, I believe this is exactly the function I’m looking for. But I have a problem, I’m with the following function CASE B1_UM WHEN 'UN' THEN D2_QUANT / B5_QE1 ELSE D2_QUANT END AS CALC_QUANT apparently it should work but DB is not recognizing the alias and is returning the error Error : 904 - ORA-00904: "CALC_QUANT": invalid identifier

  • put the full query

  • @Mhac I think in your case the problem is that you put the column that is the result of the case, CALC_QUANT, in the group by. This is another problem other than what was dealt with in this issue, you cannot group by a column that only exists in the output resultset. Whether clustering should be in another field (D2_QUANT ) or use a clamping function in that column.

  • Diego I’m really starting with SQL development, could you tell me how I can solve this problem?

  • @Actually I already said. But I made a typo: Your grouping should be in another field (D2_QUANT) or use a clamping function (such as SUM) in that column. This subject is not so trivial to be discussed in comments, if you still do not know sql... If I find here another answer that explains this in more detail I will reference in the question.

  • I got it, I thought it was simpler to do... I used excel as an example, because in excel this is a very simple function. Thanks for the help, if you get the reference thank you.

  • @Mhac but the IF is simple. And it’s already been done, including. You’re having problems is in the GROUP BY. It’s something else.

  • @Mhac Take about 10 minutes to read this post. You are addressing this issue well (GROUP BY).

  • 1

    Thanks Diego, I managed to solve the problem. As I was running a formula I should have put inside a function, in case SUM was just put the function before the logical test that worked.

Show 4 more comments

-1

What you seek I believe this to be

select iif(sexo = 0, 'Feminino', 'masculino') from clientes

Edited

I corrected the command above

As quoted at that link, the IIF(condition, answer, answer) command should work if your SQL SERVER is 2012+

  • 1

    That one query does not work in the SQL Server. I suggest waiting for more information on the question before answering...

  • what information you need?

  • It is... I will start waiting for more information, @Mhac has already modified its question...

  • @Matheusribeiro I want the query to make a calculation based on a logic test, I talked to a friend and I arrived at this result CASE B1_UM WHEN 'UN' THEN D2_QUANT / B5_QE1 ELSE D2_QUANT END AS CALC_QUANT apparently it should work but DB is not recognizing the alias we created is returning the error Error : 904 - ORA-00904: "CALC_QUANT": invalid identifier

  • Show your entire script if possible

  • @Matheusribeiro put in full

Show 1 more comment

Browser other questions tagged

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