Skip to content
Advertisement

Oracle SQL if statement?

how would it be possible to check if a certain value in salesman_id is null, and if it is, assign it to something else (in this case, 0)?

here’s what i’ve wrote up so far:

SELECT O.SALESMAN_ID, SUM(OI.UNIT_PRICE * QUANTITY)
FROM ORDERS O, ORDER_ITEMS OI
GROUP BY O.SALESMAN_ID
ORDER BY O.SALESMAN_ID;

Advertisement

Answer

I would suggest:

SELECT COALESCE(O.SALESMAN_ID, 0) as SALESMAN_ID, SUM(OI.UNIT_PRICE * QUANTITY)
FROM ORDERS O JOIN
     ORDER_ITEMS OI
     ON o.ORDER_ID = OI.ORDER_ID. -- guessing at the relationship
GROUP BY COALESCE(O.SALESMAN_ID, 0)
ORDER BY COALESCE(O.SALESMAN_ID, 0);

Your query as written would produce non-sensical results. Always use proper, explicit, standard, readable JOIN syntax. Never use commas in the FROM clause.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement