Received: from localhost (unknown [200.46.204.183]) by developer.postgresql.org (Postfix) with ESMTP id DB0462E002E for ; Mon, 31 Mar 2008 09:02:26 -0300 (ADT) Received: from developer.postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 48515-06 for ; Mon, 31 Mar 2008 09:02:26 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from mail.sanbi.ac.za (polka2.sanbi.ac.za [196.38.142.121]) by developer.postgresql.org (Postfix) with ESMTP id B2A802E002C for ; Mon, 31 Mar 2008 09:02:25 -0300 (ADT) Received: from localhost.localdomain (this163.sanbi.ac.za [192.168.2.163]) by mail.sanbi.ac.za (Postfix) with ESMTP id DC8A4AA2E99; Mon, 31 Mar 2008 14:01:58 +0200 (SAST) Message-ID: <47F0B5DA.7020101@sanbi.ac.za> Date: Mon, 31 Mar 2008 11:58:50 +0200 From: Allan Kamau User-Agent: Thunderbird 2.0.0.12 (X11/20080226) MIME-Version: 1.0 To: Craig Ringer CC: pgsql-sql@postgresql.org Subject: Re: Removing redundant itemsets References: <47F09DD1.1080905@sanbi.ac.za> <47F0C2A8.9010008@postnewspapers.com.au> In-Reply-To: <47F0C2A8.9010008@postnewspapers.com.au> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-SANBI-MailScanner-Information: Please contact the ISP for more information X-SANBI-MailScanner: Found to be clean X-SANBI-MailScanner-SpamCheck: not spam (whitelisted), SpamAssassin (not cached, score=-1.8, required 5, autolearn=not spam, ALL_TRUSTED -1.80) X-SANBI-MailScanner-From: allank@sanbi.ac.za X-Archive-Number: 200803/207 X-Sequence-Number: 30535 Craig, Thank you so much for the solution. I have spent many hours since Thursday last week including the weekend (and it took you just a few minutes) trying to figure out a solution not involving procedural programming and looping (as the size of the items and even the number of "purchases" in the datasets I may be working with may be large), I was looking for a solution that may take (almost) polynomial time (and resources) and also make use of Postgresql refined and efficient engine. Your solution satisfies these requirements. Thanks. Allan. Craig Ringer wrote: > Allan Kamau wrote: > >> Hi all, >> I have a list of purchases (market basket) and I would like to select >> non redundant longest possible patterns by eliminating >> (creating/populating other table to contain only non redandant itemsets) >> purchases having item lists which are fully included in at least one >> other purchase. >> > > Here's a possibly slow and surely ugly solution (I think it's right, > though I haven't done more than passing testing): > > > > CREATE VIEW togo_as_arr AS > SELECT a.tid, > ARRAY(SELECT item FROM togo b WHERE b.tid = a.tid ORDER BY item) > AS items > FROM togo a GROUP BY tid; > > SELECT arr_a.tid AS redundant_tid, arr_b.tid AS contained_by > FROM togo_as_arr arr_a CROSS JOIN togo_as_arr arr_b > WHERE arr_a.tid <> arr_b.tid AND arr_a.items <@ arr_b.items; > > > > (the view isn't necessary, but does improve the readability of the query). > > It groups the purchases up with item lists as arrays, then finds any > purchases with items arrays wholly contained by other item arrays from > other purchases. > > I'm *sure* there's a smarter way to do this that avoids the use of > arrays, but I don't seem to be able to come up with one right now. It's > interesting, though, so I might keep fiddling. > > -- > Craig Ringer >