X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (developer.postgresql.org [64.117.224.193]) by svr1.postgresql.org (Postfix) with ESMTP id 3A68FF006C for ; Sun, 8 Jun 2003 01:59:23 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (svr1.postgresql.org [64.117.224.193]) (amavisd-new, port 10024) with ESMTP id 40689-03 for ; Sun, 8 Jun 2003 01:59:12 +0000 (GMT) Received: from biglumber.com (biglumber.com [207.228.252.42]) by svr1.postgresql.org (Postfix) with SMTP id 56844F006E for ; Sun, 8 Jun 2003 01:58:28 +0000 (GMT) Received: (qmail 32220 invoked from network); 8 Jun 2003 01:58:32 -0000 Received: from biglumber.com (HELO localhost) (207.228.252.42) by 0 with SMTP; 8 Jun 2003 01:58:32 -0000 From: greg@turnstep.com To: pgsql-sql@postgresql.org Cc: cgg007@yahoo.com Cc: cgg007@yahoo.com Subject: Re: Elegant SQL solution: X-PGP-Key: 2529 DF6A B8F7 9407 E944 45B4 BC9B 9067 1496 4AC8 X-Request-PGP: http://www.biglumber.com/x/web?pk=2529DF6AB8F79407E94445B4BC9B906714964AC8 In-Reply-To: <20030606162635.97132.qmail@web13807.mail.yahoo.com> Date: Sun, 8 Jun 2003 01:58:32 -0000 X-Mailer: JoyMail 1.37 Message-ID: <152a2a7e34c78ff65406189821f83fa5@biglumber.com> X-Archive-Number: 200306/48 X-Sequence-Number: 13806 -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 > SELECT date_part('month',rowdate) as month, count(*) as rows FROM mytable GROUP > BY month; >... > 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. The first solution is probably the best one. It does not seem that "unelegant" to me. Another way would be just to do it in the application itself. ...or you could consider this one I came up with. Use at your own risk ;) SELECT dos.mym AS "Month", COALESCE(uno.rc,0) AS "Total" FROM (SELECT date_part('month',rowdate) AS mym, count(*) AS rc FROM mytable GROUP BY 1) AS uno RIGHT JOIN (SELECT oid::integer-15 AS mym FROM pg_type ORDER BY oid ASC LIMIT 12) AS dos USING (mym); - -- Greg Sabino Mullane greg@turnstep.com PGP Key: 0x14964AC8 200306072131 -----BEGIN PGP SIGNATURE----- Comment: http://www.turnstep.com/pgp.html iD8DBQE+4phlvJuQZxSWSsgRAqLRAJsGr5YNiGXKoXBOWq6+3OpSZXOG3ACdFr2F ywb1tBYllZt6CKtKYhoc7G4= =6yvp -----END PGP SIGNATURE-----