Re: Controlling order of evaluation?

From: Andre Maasikas <andre(dot)maasikas(at)abs(dot)ee>
To: Jerry LeVan <jerry(dot)levan(at)eku(dot)edu>
Cc: Postgres General <pgsql-general(at)postgresql(dot)org>
Subject: Re: Controlling order of evaluation?
Date: 2004-09-29 05:15:07
Message-ID: 415A44DB.7080409@abs.ee
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Jerry LeVan wrote:

> I have hoped that
>
> select * from annual_report(2003)
> union
> select * from annual_profit_loss(2003)
>
> would print the last select last ;( but it inserts the last selection
> alphabetically into the rows of the annual_report depending on the label
> field... I suppose I could use a label "zzGrand Totals", but that just
> does not look right.

I guess that it's the UNION operator that needs to elimiate duplicate rows
by definition and therefore does sorting to do this. From your
description it seems that you could use UNION ALL instead which might
work ok for you.

> Is there any way I can force the last select to appear last?
>
AFAIK the only sure way is to use ORDER BY if you have
some suitable column or can make one up. Usually using expressions
one can do pretty complicated stuff but it seems that in case of
UNION only select list items are allowed in ORDER BY. But then again you
can do it as subselect and then ....

select a from (
select a,b from ...
union all
select a,b from ...
) u
order by case when b='Last' then 'zzzz' else b end;

Andre

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Stephan Szabo 2004-09-29 05:30:55 Re: Indexes on Expressions -- Parentheses
Previous Message Thomas F.O'Connell 2004-09-29 03:44:28 Indexes on Expressions -- Parentheses