Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Data Bases > Pgsql Sql > Re: extracting ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 2 of 6 Topic 3429 of 3799
Post > Topic >>

Re: extracting words

by andreas.kretschmer@[EMAIL PROTECTED] ("A. Kretschmer") Apr 23, 2008 at 07:18 AM

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

-- 
Sent via pgsql-sql mailing list (pgsql-sql@[EMAIL PROTECTED]
)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql
 




 6 Posts in Topic:
extracting words
postgresql@[EMAIL PROTECT  2008-04-22 22:25:04 
Re: extracting words
andreas.kretschmer@[EMAIL  2008-04-23 07:18:44 
Re: extracting words
alvherre@[EMAIL PROTECTED  2008-04-23 09:13:03 
Re: extracting words
abaqueiro <abaqueiro@[  2008-04-24 09:07:33 
Re: extracting words
postgresql@[EMAIL PROTECT  2008-04-23 15:57:35 
Re: extracting words
postgresql@[EMAIL PROTECT  2008-04-24 17:12:41 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Mon Dec 1 21:26:09 CST 2008.