Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 9736E1337B2F for ; Fri, 3 Dec 2010 17:00:46 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 28554-05 for ; Fri, 3 Dec 2010 21:00:39 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from smtp.iq.pl (smtp.iq.pl [86.111.241.19]) by mail.postgresql.org (Postfix) with ESMTP id 0F1A113364DE for ; Fri, 3 Dec 2010 17:00:37 -0400 (AST) Received: (qmail 5854 invoked from network); 3 Dec 2010 21:00:37 -0000 Received: from unknown (HELO if-then-else.pl) (if-then-else_mike@[213.156.126.21]) (envelope-sender ) by iq.pl with SMTP for ; 3 Dec 2010 21:00:37 -0000 Date: Fri, 3 Dec 2010 22:00:36 +0100 From: =?UTF-8?B?TWljaGHFgg==?= Roszka To: pgsql-sql@postgresql.org Subject: A note on how to generate a list of months within some period of time. Message-Id: <20101203220036.f90bf9eb.mike@if-then-else.pl> X-Mailer: Sylpheed 3.0.2 (GTK+ 2.20.1; i486-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.6 tagged_above=-5 required=5 tests=BAYES_00=-1.9, RCVD_IN_DNSWL_LOW=-0.7 X-Spam-Level: X-Archive-Number: 201012/23 X-Sequence-Number: 34859 Hello, I am referring to an old message and it's follow-ups. Today I went through a similar problem and solved it in a different way. I would like to share my approach here to help others who might be searching for it. I think it is very simple and flexible. The old message is here: http://archives.postgresql.org/pgsql-sql/2010-03/msg00084.php Esentially, I need to generate an ordered list of months, like: 2010-12 2010-11 2010-10 ... 2008-04 2008-03 2008-02 First, let's specify the limits: the current date (max_date) and some date in the past (min_date). Additionally let's have them rounded down to the first day of the month. max_date =3D date_trunc('month', current_date); -- 2010-12-01 min_date =3D '2008-02-01'::date; -- 2008-02-01 You might need the min_date to be calculated based on the actual data: SELECT INTO min_date date_trunc('month', mytable.created) FROM mytable ORDER BY mytable.created ASC LIMIT 1; mytable.created is a column in mytable of type date (or similar) and here we select the lowest value. Replace ASC with DESC to select the highest one. Let's generate a descending list: LOOP RAISE NOTICE '%', max_date; max_date =3D max_date - '1 month'::interval; EXIT WHEN max_date < min_date; END LOOP; Replace the RAISE NOTICE statement with one, that fits your needs best. Note, that the EXIT WHEN statement solves the case when the initial value of max_date is already lower than the one of min_date. If you need an ascending list, use min_date instead of max_date and modify it by addition instead of subtraction. Leave the EXIT WHEN statement unchanged: LOOP RAISE NOTICE '%', min_date; min_date =3D min_date + '1 month'::interval; EXIT WHEN max_date < min_date; END LOOP; You get the idea. ;) Cheers, -Mike --=20 Micha=C5=82 Roszka mike@if-then-else.pl