pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Chris Gamache <cgg007@yahoo.com>
To: pgsql-sql@postgresql.org
Subject: Elegant SQL solution:
Date: Fri, 6 Jun 2003 09:26:35 -0700 (PDT)
Message-ID: <20030606162635.97132.qmail@web13807.mail.yahoo.com> (raw)
In-Reply-To: <20030606122001.GA30097@wolff.to>

There are so many (bad) ways to skin this cat... I'm looking for a more elegant
solution.

If I

SELECT date_part('month',rowdate) as month, count(*) as rows FROM mytable GROUP
BY month;

It might only return

 month | rows
-------+------
 1     | 234
 3     | 998
 4     | 403
 5     | 252
 10    | 643
 12    | 933

I would like:

 month | rows
-------+------
 1     | 234
 2     | 0
 3     | 998
 4     | 403
 5     | 252
 6     | 0
 7     | 0
 8     | 0
 9     | 0
 10    | 643
 11    | 0
 12    | 933


I could create a one-column table with values 1 - 12 in it, and select from
that table with a where clause matching "month". I could also create a view 
"SELECT 1 UNION SELECT 2 UNION ..." and select against the view. There MUST be
a more elegant way to do this.

Any thoughts?


__________________________________
Do you Yahoo!?
Yahoo! Calendar - Free online calendar with sync to Outlook(TM).
http://calendar.yahoo.com



view thread (4+ messages)  latest in thread

Message-ID: <20030606162635.97132.qmail@web13807.mail.yahoo.com>
Permalink:  ../20030606162635.97132.qmail@web13807.mail.yahoo.com/
Also on:    postgresql.org/message-id/20030606162635.97132.qmail@web13807.mail.yahoo.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: cgg007@yahoo.com
  Subject: Re: Elegant SQL solution:
  In-Reply-To: <20030606162635.97132.qmail@web13807.mail.yahoo.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