Although this technique works great for standard columns, I have run into something I don't understand. I am rewriting results in my query for reporting purposes and need to order by the aliased column 'PARENT', like so:
select fun.MODULE_ID
, fun.FUNCTION_ID
, COALESCE(fun.parent_function, fun2.function_id) as PARENT
, fun.DESCRIPTION
, fun.FUNCTION_PURPOSE
from MODULE_FUNCTION fun
LEFT JOIN MODULE_FUNCTION fun2
ON fun.function_id = fun2.function_id
AND fun2.function_id IN (SELECT parent_function FROM MODULE_FUNCTION)
ORDER BY MODULE_ID
, PARENT
, FUNCTION_ID
This works great, but my column 'PARENT_FUNCTION' and its alias 'PARENT' contain nulls. I would like these to be at the bottom. So when I do this:
ORDER BY MODULE_ID
, case when PARENT is null then 1 else 0 end, PARENT
, FUNCTION_ID
I get " Invalid column name 'PARENT'. "
Any ideas? (Still very new with SQL!)