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 372623A513B for ; Tue, 30 Nov 2004 09:25:43 +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 26121-08 for ; Tue, 30 Nov 2004 09:25:30 +0000 (GMT) Received: from anchor-post-36.mail.demon.net (anchor-post-36.mail.demon.net [194.217.242.86]) by svr1.postgresql.org (Postfix) with ESMTP id 647143A513D for ; Tue, 30 Nov 2004 09:25:31 +0000 (GMT) Received: from mwynhau.demon.co.uk ([193.237.186.96] helo=mainbox.archonet.com) by anchor-post-36.mail.demon.net with esmtp (Exim 4.42) id 1CZ4GF-000C8h-Lb; Tue, 30 Nov 2004 09:25:28 +0000 Received: from [192.168.1.17] (client17.archonet.com [192.168.1.17]) by mainbox.archonet.com (Postfix) with ESMTP id 8292215F04; Tue, 30 Nov 2004 09:25:18 +0000 (GMT) Message-ID: <41AC3C7E.10201@archonet.com> Date: Tue, 30 Nov 2004 09:25:18 +0000 From: Richard Huxton User-Agent: Mozilla Thunderbird 0.9 (X11/20041103) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Johan Henselmans Cc: pgsql-sql@postgresql.org Subject: Re: grouping a many to many relation set References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200411/299 X-Sequence-Number: 19617 Johan Henselmans wrote: > Hi, I am having a problem grouping a many to many relationship with > payments and receipts, where a payment can be for multiple receipts, and > a receipt can have multiple payments. I got a list of records that are > involved in such relations, but now I don't know how to group them so > that all payments and rececipts belonging to the same group are properly > grouped. Here's the list: > > > bankbookdetid | receiptid > ---------------+----------- > 147 | 25 > 157 | 25 > 157 | 622 > > 321 | 100 > 332 | 101 > 332 | 100 ... I think what's missing here is the explicit statement of which group these belong in. Without a value to sort/group by, there's nothing for your queries to "get a grip on". So - add a "group_id" column to the bank-book and receipt tables. Create a sequence to generate group id's on demand. Then you'll want a set of triggers that keeps the group details up to date. Of course, groups can shift as you add more records - particularly in the case of two groups merging when you add a "linking" row. Maybe someone smarter than me can come up with a non-procedural solution. Personally, I've got a nagging feeling that this sort of "connectedness" problem is NP, so scaling could be a problem for you. -- Richard Huxton Archonet Ltd