Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Txz9A-0001fc-9v for pgsql-sql@arkaria.postgresql.org; Wed, 23 Jan 2013 12:09:40 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1Txz99-0006Vr-R5 for pgsql-sql@arkaria.postgresql.org; Wed, 23 Jan 2013 12:09:39 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Txz98-0006Vl-Nr for pgsql-sql@postgresql.org; Wed, 23 Jan 2013 12:09:38 +0000 Received: from mout.gmx.net ([212.227.17.20]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Txz96-00043U-70 for pgsql-sql@postgresql.org; Wed, 23 Jan 2013 12:09:38 +0000 Received: from mailout-de.gmx.net ([10.1.76.16]) by mrigmx.server.lan (mrigmx002) with ESMTP (Nemesis) id 0LfDpm-1T9HAl1sOO-00oolj for ; Wed, 23 Jan 2013 13:09:35 +0100 Received: (qmail invoked by alias); 23 Jan 2013 12:09:35 -0000 Received: from mue-88-130-23-112.dsl.tropolys.de (EHLO [192.168.1.113]) [88.130.23.112] by mail.gmx.net (mp016) with SMTP; 23 Jan 2013 13:09:35 +0100 X-Authenticated: #14269776 X-Provags-ID: V01U2FsdGVkX19aVQR3KppaUfREI67lEY07TAUL7QAFgMNGRXDtUo RcMTFq3d/orqUf Message-ID: <50FFD38B.50301@gmx.net> Date: Wed, 23 Jan 2013 13:11:55 +0100 From: Andreas User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/20130107 Thunderbird/17.0.2 MIME-Version: 1.0 To: Alexander Gataric CC: =?UTF-8?B?RmlsaXAgUmVtYmlhxYJrb3dza2k=?= , pgsql-sql@postgresql.org Subject: Re: Re: [SQL] need some magic with generate_series() References: <695Rawai22960M34@ca34.cms.usa.net> In-Reply-To: <695Rawai22960M34@ca34.cms.usa.net> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Y-GMX-Trusted: 0 X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org I'm sorry to prove that daft. :( generate_series needs the startdate of every project to generate the specific list of monthnumbers for every project. To join against this the list needs to have a column with the project_id. So I get something like this but still I cant reference the columns of the projects within the query that generates the series. with projectstart ( project_id, startdate ) as ( select project_id, startdate from projects ) select project_id, m from projectstart as p left join ( select p.project_id, to_char ( m, 'YYYYMM' )::integer from generate_series ( p.startdate, current_date, '1 month'::interval ) as m ) as x using ( project_id ); Am 23.01.2013 01:08, schrieb Alexander Gataric: > I would create a common table expression with the series from Filip > and left join to the table you need to report on. > > Sent from my smartphone > > ----- Reply message ----- > From: "Andreas" > To: "Filip RembiaƂkowski" > Cc: "jan zimmek" , > Subject: [SQL] need some magic with generate_series() > Date: Tue, Jan 22, 2013 4:49 pm > > > Thanks Filip, > with your help I came a step further. :) > > Could I do the folowing without using a function? > > > CREATE OR REPLACE FUNCTION month_series ( date ) > RETURNS table ( monthnr integer ) > AS > $BODY$ > > select to_char ( m, 'YYYYMM' )::integer > from generate_series ( $1, current_date, '1 month'::interval ) > as m > > $BODY$ LANGUAGE sql STABLE; > > > select project_id, month_series ( createdate ) > from projects > order by 1, 2; > > > > Am 22.01.2013 22:52, schrieb Filip RembiaƂkowski: > > or even > > > > select m from generate_series( '20121101'::date, '20130101'::date, '1 > > month'::interval) m; > > > > > > > > On Tue, Jan 22, 2013 at 3:49 PM, jan zimmek wrote: > >> hi andreas, > >> > >> this might give you an idea how to generate series of dates (or > other datatypes): > >> > >> select g, (current_date + (g||' month')::interval)::date from > generate_series(1,12) g; > >> > >> regards > >> jan > >> > >> Am 22.01.2013 um 22:41 schrieb Andreas : > >> > >>> Hi > >>> I need a series of month numbers like 201212, 201301 YYYYMM to > join other sources against it. > >>> > >>> I've got a table that describes projects: > >>> projects ( id INT, project TEXT, startdate DATE ) > >>> > >>> and some others that log events > >>> events( project_id INT, createdate DATE, ...) > >>> > >>> to show some statistics I have to count events and present it as a > view with the project name and the month as YYYYMM starting with > startdate of the projects. > >>> > >>> My problem is that there probaply arent any events in a month but > I still need this line in the output. > >>> So somehow I need to have a select that generates: > >>> > >>> project 7,201211 > >>> project 7,201212 > >>> project 7,201301 > >>> > >>> It'd be utterly cool to get this for every project in the projects > table with one select. > >>> > >>> Is there hope? > >>> > >>> > >>> -- > >>> Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) > >>> To make changes to your subscription: > >>> http://www.postgresql.org/mailpref/pgsql-sql > >> > >> > >> -- > >> Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) > >> To make changes to your subscription: > >> http://www.postgresql.org/mailpref/pgsql-sql > > > > -- > Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) > To make changes to your subscription: > http://www.postgresql.org/mailpref/pgsql-sql -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql