Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IF ELSE condition on ORACLE

I'm trying to make a sql request on Oracle but i'hve a little probleme. I've this sentence to translate and I don't know how.. Can you help me ? How can I translate it ? :(

IF(CODE="fuu")
THEN(SELECT bar FROM TABLE)
ELSE(we return 0)

Thx in advice.

EDIT :

My request is like that

SELECT distinct
  A, B, C, D, E,
  SUM(F+G+H) AS "FGH",
  SUM(I -(FGH)) AS "I",
  CASE WHEN code='fuu' THEN bar ELSE 0 END AS AS "What I need"
FROM
  TABLE
WHERE 
  A=20
GROUP BY
  A, B, C, D, E;

This solution works but i'don't have the good answer for it. Actually is should have for A=20 -> bar = 7598 and not 0;

like image 213
Drupal8ForTheWin Avatar asked Feb 21 '14 13:02

Drupal8ForTheWin


People also ask

Can we use else if in PLSQL?

An IF-THEN statement can be followed by an optional ELSIF...ELSE statement.

What is conditional statement in Oracle?

Conditional selection statements, which run different statements for different data values. The conditional selection statements are IF and CASE . Loop statements, which run the same statements with a series of different data values. The loop statements are the basic LOOP , FOR LOOP , and WHILE LOOP .

How many else clauses can an if statement in PLSQL have?

An IF statement can have any number of ELSIF clauses; the final ELSE clause is optional.


1 Answers

A case expression in SQL is an equivalent to IF statement in other languages:

CASE WHEN condition THEN value1 [ ELSE value2 ] END

I guess that you are looking for something like this:

SELECT distinct
  A, B, C, D, E,
  SUM(F+G+H) AS "FGH",
  SUM(I -(FGH)) AS "I",
  CASE WHEN CODE='fuu' THEN bar ELSE 0 END As "What I need"
FROM
  TABLE
WHERE 
  A=20
GROUP BY
  A, B, C,  D, E, "What I need"
like image 97
krokodilko Avatar answered Oct 12 '22 02:10

krokodilko