Subject Re: [firebird-support] Creating a conditional "order by" statement
Author Tomasz Tyrakowski
On 23.10.2017 at 14:37, junk@... [firebird-support] wrote:
> I have a stored procedure along the lines of
> SELECT ID, Amount1, Amount2 ..
> FROM Table1.
> UNION
> SELECT ID, Amount1, Amount2...
> FROM Table2
> ORDER BY 1
> Is it possible to construct a conditional ORDER By clause that orders by Amount1 under certain conditions, and order by Amount2 under other conditions?
>

Try something like this:

select iif(your condition, Amount1, Amount2) as SORTCOL, ID, Amount1,
Amount2, ...
from Table1
union
select iif(your condition, Amount1, Amount2) as SORTCOL, ID, Amount1,
Amount2, ...
from Table2
order by 1

If there are more than two possible candidates for the ordering column,
you can switch from iif to case - when - end.

have a good one
Tomasz

--
__--==============================--__
__--== Tomasz Tyrakowski ==--__
__--== SOL-SYSTEM ==--__
__--== http://www.sol-system.pl ==--__
__--==============================--__