Re: "UNION ALL" is failing

From: Darren Duncan <darren(at)darrenduncan(dot)net>
To: Joy Smith <freestuffanddeals(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: "UNION ALL" is failing
Date: 2011-08-28 05:38:49
Message-ID: 4E59D469.80509@darrenduncan.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

The whole point of "with" is to factor out redundancy, and yet here you are
going and repeating the 2 "with" declarations; also the declarations have the
same names, which would be a problem, besides being redundant.

Try it like this instead:

with ...
(select ...)
union all
(select ...)
order by node

But moreover, I think you can avoid the union and doubled main selects, by
saying this instead:

with a as
(
select channel,node,accesses from storage where monthly = '11-06'
),
b as
(
select channel,node,accesses from storage where monthly = '11-07'
)
select
b.node
from a right join b on a.node=b.node
where a.accesses is null and b.channel in ('611 IVR', 'olam')
order by node

-- Darren Duncan

Joy Smith wrote:
> with a as
> (
> select channel,node,accesses from storage where monthly = '11-06'
> ),
> b as
> (
> select channel,node,accesses from storage where monthly = '11-07'
> )
>
> select
> b.node
> from a right join b on a.node=b.node
> where a.accesses is null and b.channel = ('611 IVR')
> order by node
>
>
> UNION ALL
>
>
> with a as
> (
> select channel,node,accesses from storage where monthly = '11-06'
> ),
> b as
> (
> select channel,node,accesses from storage where monthly = '11-07'
> )
>
>
> select
> b.node
> from a right join b on a.node=b.node
> where a.accesses is null and b.channel = 'olam'
> order by node

In response to

Browse pgsql-general by date

  From Date Subject
Next Message pasman pasmański 2011-08-28 05:53:33 Re: Problem importing a csv file
Previous Message planas 2011-08-28 05:03:53 Problem importing a csv file