Trying to get a return of lowest # based on 2 fields. This returns the wrong amount of 3 when 2 need 2 in the check field.
This is my original code that runs but returns the wrong amount.
select location,unitofstorage,depth,
SUM(CASE WHEN depth=pallets then depth else to_char(pallets) end) AS "CHECK"
FROM db
WHERE facility IN ('XXX4')
GROUP BY location,unitofstorage,depth
ORDER BY location
This is the result. CHECK should be 2.

Somebody said to try this.... But getting the ORA-00933: SQL command not properly ended. I suspect it is something simple but I have not found anything to help as of yet. I simplified this version.
select a.*,
CASE WHEN a.pallets<=a.depth then to_char(a.pallets) else a.depth end AS "CHECK"
FROM db
WHERE (facility IN ('xxx4')
AND unitofstorage<>'NA')a;
ORA-00933: SQL command not properly ended 00933. 00000 - "SQL command not properly ended" *Cause:
*Action: Error at Line: 1 Column: 354