i have a problem with my sql query and dont know how to solve it:
The output table consists of.
1. SERIALNR VARCHAR(26) NOT NULL
2. LP_BASIC VARCHAR(20) NOT NULL
3. LP_NEW VARCHAR(20)
4. SCORE NUMBER(20) NOT NULL
5. CURRENT_LP VARCHAR(20)
The query itself works well and contains no errors.
The LP_BASIC column is always filled whereas the LP_NEW is an optional field. CURRENT_LP should always display LP_NEW (if is not empty) else LP_BASIC (which is never empty).
The problem is, that the "ELSE f.LP_BASIC"-Part is not working. It should return the LP_BASIC value, if no LP_NEW record is found but instead it only returns an empty field.
SELECT
f.SERIALNR,
f.LP_BASIC,
f.LP_NEW,
f.SCORE,
CASE
WHEN
f.LP_NEW
IS NOT NULL
THEN
f.LP_NEW
ELSE
f.LP_BASIC
END
AS CURRENT_LP
FROM (
SELECT
SERIALNR,
MAX(SCORE) MAX_ID
FROM
EAPS.PS_TC_SM
GROUP BY
SERIALNR
) x
INNER JOIN
EAPS.PS_TC_SM f
ON
f.SERIALNR=
x.SERIALNR
AND
f.SCORE =
x.MAX_ID
Any help apreciated.
Thank you in advance.
Rob.
UPDATE (solved)
The LP_NEW column was not null, there was a single space char in it, so the "IS NULL" did not work.
coalesce(f.LP_NEW ,f.LP_BASIC)Coalesceis better.Nvlcompares both values,coalescestops as soon as a value is found.NVL()norCOALESCE()will solve the problem. It would be very weird ifNVL()orCOALESCE()would fix anythingCASEdoes wrong, since they are logically equivalent - with the difference in implementation that makesCASEandCOALESCE()a little faster thanNVL()in some cases.