Received: from localhost (unknown [200.46.204.184]) by developer.postgresql.org (Postfix) with ESMTP id 6D8892E0048 for ; Wed, 23 Apr 2008 10:13:30 -0300 (ADT) Received: from developer.postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 50404-01 for ; Wed, 23 Apr 2008 10:13:09 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from lists.commandprompt.com (host-159.commandprompt.net [207.173.203.159]) by developer.postgresql.org (Postfix) with ESMTP id 99A0A2E0037 for ; Wed, 23 Apr 2008 10:13:17 -0300 (ADT) Received: from perhan.alvh.no-ip.org (200-126-123-196.bk8-dsl.surnet.cl [200.126.123.196]) (authenticated bits=0) by lists.commandprompt.com (8.13.8/8.13.8) with ESMTP id m3NDDjnv028334 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Wed, 23 Apr 2008 06:13:47 -0700 Received: by perhan.alvh.no-ip.org (Postfix, from userid 1000) id 2473047C59; Wed, 23 Apr 2008 09:13:03 -0400 (CLT) Date: Wed, 23 Apr 2008 09:13:03 -0400 From: Alvaro Herrera To: Tarlika Elisabeth Schmitz Cc: pgsql-sql@postgresql.org Subject: Re: extracting words Message-ID: <20080423131303.GE4768@alvh.no-ip.org> References: <20080422222504.56bdb06d@dick.coachhouse> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20080422222504.56bdb06d@dick.coachhouse> User-Agent: Mutt/1.5.17+20080114 (2008-01-14) X-Greylist: Sender succeeded SMTP AUTH, not delayed by milter-greylist-4.0 (lists.commandprompt.com [207.173.203.159]); Wed, 23 Apr 2008 06:13:48 -0700 (PDT) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200804/147 X-Sequence-Number: 30686 Tarlika Elisabeth Schmitz wrote: > I need to extract the words from department_name and product_name > (words are separated by spaces) and get something like: > > 1 cakes > 1 desserts > 1 apple > 1 crumble > 2 cakes > 2 desserts > 2 cheese > 2 cake > 3 starters > 3 soups > 3 french > 3 onion > 3 soup regexp_split_to_table is handy (8.3 only): select pk, regexp_split_to_table(product_name, '[& ]+') from products union select pk, regexp_split_to_table(dept_name, '[& ]+') from departments; Add joins as desired. -- Alvaro Herrera http://www.CommandPrompt.com/ PostgreSQL Replication, Consulting, Custom Development, 24x7 support