Received: from localhost (unknown [200.46.204.184]) by developer.postgresql.org (Postfix) with ESMTP id 317B22E00C6 for ; Wed, 23 Apr 2008 02:26:26 -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 89942-02-3 for ; Wed, 23 Apr 2008 02:26:17 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from moutng.kundenserver.de (moutng.kundenserver.de [212.227.126.187]) by developer.postgresql.org (Postfix) with ESMTP id 085482E008E for ; Wed, 23 Apr 2008 02:18:47 -0300 (ADT) Received: from webserv.wug.de ([194.25.154.138]) by mrelayeu.kundenserver.de (node=mrelayeu7) with ESMTP (Nemesis) id 0ML2xA-1JoXNV0ZZs-0003nA; Wed, 23 Apr 2008 07:18:46 +0200 Received: from kretschmer by webserv.wug.de with local (Exim 3.36 #1) id 1JoXNU-0002Jy-00 for pgsql-sql@postgresql.org; Wed, 23 Apr 2008 07:18:44 +0200 Date: Wed, 23 Apr 2008 07:18:44 +0200 From: "A. Kretschmer" To: pgsql-sql@postgresql.org Subject: Re: extracting words Message-ID: <20080423051842.GA8401@a-kretschmer.de> 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> X-OS: Debian/GNU Linux - weil ich es mir Wert bin! User-Agent: Mutt/1.5.9i X-Provags-ID: V01U2FsdGVkX19IqrZZd54c2F+3AsDX4YJiqm/qc+7UQUy8xmy KwKebV4Mdf5SIeHMBPumvOB10L31Clo2QM4EQrwUpNmboTYQBq hOHLITr6Y/iPZsgiIQCYyIWJVeaZXpt X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200804/146 X-Sequence-Number: 30685 am Tue, dem 22.04.2008, um 22:25:04 +0100 mailte Tarlika Elisabeth Schmitz folgendes: > I am not quite sure whether this would be sensible or indeed at > all possible to do in SQL: > > TABLE product: product_pk, product_name, department_fk > TABLE product: department_pk, department_name > > example data: > > SELECT product_pk, department_name, product_name ... LEFT JOIN ...: > > 1, "cakes & desserts", "apple crumble" > 2, "cakes & desserts", "cheese cake" > 3, "starters & soups", "french onion soup" > > > 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 > A little function (author: David Fetter) -- split a string to rows, by David Fetter CREATE OR REPLACE FUNCTION split_to_rows(TEXT, TEXT) RETURNS SETOF TEXT STRICT LANGUAGE sql AS $$ SELECT (string_to_array($1, $2))[s.i] FROM generate_series( 1, array_upper(string_to_array($1, $2), 1) ) AS s(i); $$; And now: test=*# select * from product ; id | val1 | val2 ----+------------------+------------------- 1 | cakes & desserts | apple crumble 2 | cakes & desserts | cheese cake 3 | starters & soups | french onion soup (3 rows) test=*# select id, split_to_rows(replace(val1 || ' ' || val2,' &',''),' ') as col1 from product; id | col1 ----+---------- 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 (13 rows) HTH, Andreas -- Andreas Kretschmer Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header) GnuPG-ID: 0x3FFF606C, privat 0x7F4584DA http://wwwkeys.de.pgp.net