X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id A117E3A5ECE for ; Thu, 27 Jan 2005 16:27:48 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 80944-07 for ; Thu, 27 Jan 2005 16:27:44 +0000 (GMT) Received: from rproxy.gmail.com (rproxy.gmail.com [64.233.170.204]) by svr1.postgresql.org (Postfix) with ESMTP id 39A293A5EB2 for ; Thu, 27 Jan 2005 16:27:41 +0000 (GMT) Received: by rproxy.gmail.com with SMTP id a36so259863rnf for ; Thu, 27 Jan 2005 08:27:40 -0800 (PST) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:reply-to:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:references; b=askPc8msrb3Dx/qyQ+AHJLtXMOXWude8rSgIqLPGvRTcrRQAsJRa+5ClMjmEP9zkOEWiyb08XtKyn+s8Zjni+2yzHEA869HQAYj8vSb0rq/hBWMuZwwga9bndKejVkLQkc3E3pxKTxg8XT/WDgm5tos6jiy3duR5sYR3OwC88oM= Received: by 10.38.209.66 with SMTP id h66mr7520rng; Thu, 27 Jan 2005 08:27:40 -0800 (PST) Received: by 10.38.163.47 with HTTP; Thu, 27 Jan 2005 08:27:40 -0800 (PST) Message-ID: <758d5e7f050127082755f9af12@mail.gmail.com> Date: Thu, 27 Jan 2005 17:27:40 +0100 From: Dawid Kuroczko Reply-To: Dawid Kuroczko To: Alexandre Leclerc Subject: Re: Flattening a kind of 'dynamic' table Cc: PERFORM In-Reply-To: <1dc7f0e305012707234159cfc8@mail.gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit References: <1dc7f0e305012707234159cfc8@mail.gmail.com> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.549 tagged_above=0 required=5 tests=AWL, FROM_ENDS_IN_NUMS, RCVD_BY_IP X-Spam-Level: X-Archive-Number: 200501/541 X-Sequence-Number: 10231 On Thu, 27 Jan 2005 10:23:34 -0500, Alexandre Leclerc wrote: > Here a normal listing of design.product_department_time: > product_id | department_id | req_time > ------------+---------------+---------- > 906 | A | 3000 > 906 | C | 3000 > 906 | D | 1935 > 907 | A | 1500 > 907 | C | 1500 > 907 | D | 4575 > 924 | A | 6000 > 924 | C | 1575 Well, I did something like this recently; it can be done though maybe not very efficiently... Unfortunately we will need a rowtype with all the departaments: CREATE DOMAIN departaments AS (a int, b int, c int, d int, ...); A function aggregate for this type: CREATE FUNCTION dep_agg(ds departaments, args text[]) RETURNS departaments AS $$ BEGIN IF args[1] = 'A' THEN ds.a = args[2]; -- I think it is not possible to do ds.$args[1] = args[2] equivalent. ELSIF args[1] = 'B' THEN ds.b = args[2]; ELSIF args[1] = 'C' THEN ds.c = args[2]; ELSIF args[1] = 'D' THEN ds.d = args[2]; END IF; RETURN ds; END; $$ LANUGAGE plpgsql; THEN an aggregate: CREATE AGGREGATE dep_aggregate (basetype = text[], stype = departaments, sfunc =dep_agg); AND then a view for sugar: CREATE VIEW prod_dep_time VIEW AS SELECT product_id, (dep_aggregate(ARRAY[departament_id, req_time]::text[])).* FROM product_department_time GROUP BY product_id; And voila. :) Couple of comments: -- aggregate takes array[] since making "multicolumn" aggregates is not possible, as far as I know. -- I did not check the code, yet I did manage to make it work some time before. You may need to use "ROWS" or something in the function definition; I don't remember and can't check it right now. -- comments welcome. :) Regards, Dawid