Let's say you have an Orders Table and an Order details table. (With a
linking column of OrderNo)

And by executing this SQL statement:

Select t1.OrderNo, sum(t2.OrderDetailAmt) +
>From Orders t1, OrderDetail t2 +
Where t1.OrderNo = t2.OrderNo +
And t1.CustomerNo = 'ABC' +
Group by t1.OrderNo

You'll get a list of orders for customer 'ABC' with the totals of each
order.


But, let's say you want to sort the result set of the above select by the
totals of each order.

You can either create a view or project the result set to a temporary table.


You can however modify the select to this:

Select t1.OrderNo, sum(t2.OrderDetailAmt)  AS tmpOrderTotal+
>From Orders t1, OrderDetail t2 +
Where t1.OrderNo = t2.OrderNo +
And t1.CustomerNo = 'ABC' +
Group by t1.OrderNo +
Order by tmpOrderTotal

The result set will be the same as the previous select but it will be in
ascending order by the sum of the OrderDetailAmt.


You can also alias any valid expression in a select statement and sort it by
that "column".  And from the selects I've tried, you can use any name for
the alias even the same column name you used in the aggregate function.
However, I'd recommend using a unique name.


Just something I'd like to share.


Rommel

Reply via email to