Elegant SQL solution:

From: Chris Gamache <cgg007(at)yahoo(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: Elegant SQL solution:
Date: 2003-06-06 16:26:35
Message-ID: 20030606162635.97132.qmail@web13807.mail.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Michael A Nachbaur 2003-06-06 17:03:29 "Join" on delimeter aggregate query
Previous Message David Olbersen 2003-06-06 16:23:05 (long) What's the problem?