pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Joel Burton <joel@joelburton.com>
To: Ian Barwick <barwick@gmx.net>
To: Jakub Ouhrabka <jouh8664@ss1000.ms.mff.cuni.cz>
Cc: pgsql-sql@postgresql.org
Subject: Re: difficult query
Date: Sun, 5 May 2002 02:19:34 -0400
Message-ID: <JGEPJNMCKODMDHGOBKDNOENICMAA.joel@joelburton.com> (raw)
In-Reply-To: <200205042019.34265.barwick@gmx.net>


> > 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.




view thread (6+ messages)

Message-ID: <JGEPJNMCKODMDHGOBKDNOENICMAA.joel@joelburton.com>
Permalink:  ../JGEPJNMCKODMDHGOBKDNOENICMAA.joel@joelburton.com/
Also on:    postgresql.org/message-id/JGEPJNMCKODMDHGOBKDNOENICMAA.joel@joelburton.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: joel@joelburton.com, barwick@gmx.net, jouh8664@ss1000.ms.mff.cuni.cz
  Subject: Re: difficult query
  In-Reply-To: <JGEPJNMCKODMDHGOBKDNOENICMAA.joel@joelburton.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox