I have to sort out my data by some column, such that some specific value appears first. So for a query like this …
SELECT rtrim(taskid) into v_taskid FROM tasks
where
/* some where clausers */
and rownum = 1
… I have based it on case when, but what bothers me is three nested selects I have now:
SELECT rtrim(taskid) into v_taskid FROM tasks where taskid in (
select taskid from (
select taskid,
case when taskuser like '%myuser%'
then 0
else 100
end as ordervalue
FROM tasks
where
/* some where clausers */
order by ordervalue
)
)
and rownum = 1
Performance-wise I think it shouldn’t be a problem, but it looks kind of a spaghetti… Is there any way to put case-when into the where clause?
You are querying the
taskstable twice. This is not needed and will make your query run slowly. Your query can be combined into a simple query like so:Then either just retrieve the first row or add the rownum part in an outer query.