Re: difficult query

From: "Joel Burton" <joel(at)joelburton(dot)com>
To: "Ian Barwick" <barwick(at)gmx(dot)net>, "Jakub Ouhrabka" <jouh8664(at)ss1000(dot)ms(dot)mff(dot)cuni(dot)cz>
Cc: <pgsql-sql(at)postgresql(dot)org>
Subject: Re: difficult query
Date: 2002-05-05 06:19:34
Message-ID: JGEPJNMCKODMDHGOBKDNOENICMAA.joel@joelburton.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql


> > I'm curious. I've been on a sql course long time ago where the tutor
> > mentioned similiar problem as something special for sql smarties... And
> > yesterday when I faced this problem (which can be solved perfectly with
> > the outer join) I wasn't able to remember the solution... If there is
> > any... Just out of curiosity...
>
> well, you could do it like this I suppose:
>
> SELECT 1 AS day, COUNT(data) FROM table1 WHERE day=1
> UNION
> SELECT 2, COUNT(data) FROM table1 WHERE day=2
> UNION
> SELECT 3, COUNT(data) FROM table1 WHERE day=3
> UNION
> SELECT 4, COUNT(data) FROM table1 WHERE day=4
>
> (and so on ad infinitum)
>
> although I expect E.F. Codd will be turning in his grave ;-)

A little better, perhaps, but still a true hack:

select alldays.d, sum(c) from
(select 1 as d union all
select 2 union all
select 3 union all
select 4 union all
select 5 ...) as alldays
left outer join d using (d) group by alldays.d;

If you could write a function in plpgsql that returned a query result, you
could use that as the from clause rather than the long union.

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2002-05-05 17:47:02 Re: More long-string woes
Previous Message Tom Lane 2002-05-05 06:01:40 Re: More long-string woes