X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 695AA3A5CF8 for ; Thu, 13 Jan 2005 10:46:02 +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 14110-07 for ; Thu, 13 Jan 2005 10:45:50 +0000 (GMT) Received: from rproxy.gmail.com (rproxy.gmail.com [64.233.170.194]) by svr1.postgresql.org (Postfix) with ESMTP id E30D93A5C31 for ; Thu, 13 Jan 2005 10:45:52 +0000 (GMT) Received: by rproxy.gmail.com with SMTP id i8so290563rne for ; Thu, 13 Jan 2005 02:45:52 -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:in-reply-to:mime-version:content-type:content-transfer-encoding:references; b=neQgiollxYiq+drI3bi201iKr0hrvYwgJhEa2BnoeMIeeycAlg10Tn+R0BARKT7M2QoHDCtvXm/afJ957PX/Dh5q4r10zNUI5urQSosehuvSOu6pC4xJPPmICx/w5qOgslLvCbLMcBym6P8psmK9ZTEMb395Yoyn/Pm+8hrm4mo= Received: by 10.38.163.75 with SMTP id l75mr186271rne; Thu, 13 Jan 2005 02:45:52 -0800 (PST) Received: by 10.38.163.47 with HTTP; Thu, 13 Jan 2005 02:45:52 -0800 (PST) Message-ID: <758d5e7f0501130245e2e6fb1@mail.gmail.com> Date: Thu, 13 Jan 2005 11:45:52 +0100 From: Dawid Kuroczko Reply-To: Dawid Kuroczko To: pgsql-sql@postgresql.org Subject: Re: Columns into rows. In-Reply-To: <758d5e7f05011302265be7e12a@mail.gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit References: <758d5e7f05011302265be7e12a@mail.gmail.com> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.54 tagged_above=0 required=5 tests=FROM_ENDS_IN_NUMS, RCVD_BY_IP X-Spam-Level: X-Archive-Number: 200501/78 X-Sequence-Number: 19866 On Thu, 13 Jan 2005 11:26:04 +0100, Dawid Kuroczko wrote: > I can write: > > SELECT * FROM (SELECT id, path AS path_a FROM new_table_paths WHERE id > = <> AND pathtype = 'a') AS a NATURAL FULL OUTER JOIN (SELECT id, > path AS path_bb FROM new_table_paths WHERE id = <> AND pathtype = > 'bb') AS bb; [ and so on... ] > > And its fast; however if I move WHERE id = <> outside selects > (for views), it first "materializes" old layout, and then selects > id... total waste. > > SELECT * FROM (SELECT id, path AS path_a FROM new_table_paths WHERE > pathtype = 'a') AS a NATURAL FULL OUTER JOIN (SELECT id, path AS > path_bb FROM new_table_paths WHERE pathtype = 'bb') AS bb WHERE id = > <>; > > I wonder if you know other way to write this join so it has good performace? > Once again, I need a SELECT since I want a VIEW. :-) Hmm, something like a multiple column returning aggregate... As far as I know it is not possible to create aggregate which could return more than one column? I'm thinking about something like this: SELECT id,r2c_aggregate(pathtype, path) FROM new_table_paths GROUP BY id; ...where r2c_aggregate(..) would be a plpgsql function doing the pathtype+path --> path_a = path, path_bb = path2, path_ccc = path3 assembly. And it would be quite efficient I guess! :) Hmm, r2c_aggregate could return array of arrays of pathtype/path pairs, which could be processed by other function which would convert them into rows, but this is UGLY(TM). :) Regards, Dawid