pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
extracting words
5+ messages / 3 participants
[nested] [flat]

* extracting words
@ 2008-04-22 21:25  Tarlika Elisabeth Schmitz <postgresql@numerixtechnology.de>
  0 siblings, 2 replies; 5+ messages in thread

From: Tarlika Elisabeth Schmitz @ 2008-04-22 21:25 UTC (permalink / raw)
  To: pgsql-sql

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



Would it be more sensible to produce a table join and split the result
into keywords programmatically?






--


Best Regards,

Tarlika Elisabeth Schmitz


A: Because it breaks the logical sequence of discussion
Q: Why is top posting bad? 



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: extracting words
@ 2008-04-23 05:18  A. Kretschmer <andreas.kretschmer@schollglas.com>
  parent: Tarlika Elisabeth Schmitz <postgresql@numerixtechnology.de>
  1 sibling, 1 reply; 5+ messages in thread

From: A. Kretschmer @ 2008-04-23 05:18 UTC (permalink / raw)
  To: pgsql-sql

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



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: extracting words
@ 2008-04-23 13:13  Alvaro Herrera <alvherre@commandprompt.com>
  parent: Tarlika Elisabeth Schmitz <postgresql@numerixtechnology.de>
  1 sibling, 1 reply; 5+ messages in thread

From: Alvaro Herrera @ 2008-04-23 13:13 UTC (permalink / raw)
  To: Tarlika Elisabeth Schmitz <postgresql@numerixtechnology.de>; +Cc: pgsql-sql

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



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: extracting words
@ 2008-04-23 14:57  Tarlika Elisabeth Schmitz <postgresql@numerixtechnology.de>
  parent: Alvaro Herrera <alvherre@commandprompt.com>
  0 siblings, 0 replies; 5+ messages in thread

From: Tarlika Elisabeth Schmitz @ 2008-04-23 14:57 UTC (permalink / raw)
  To: Alvaro Herrera <alvherre@commandprompt.com>; +Cc: Tarlika Elisabeth Schmitz <postgresql@numerixtechnology.de>; pgsql-sql

On Wed, 23 Apr 2008 09:13:03 -0400
Alvaro Herrera <alvherre@commandprompt.com> wrote:

> 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.


This is neat!
Unfortunately, on the machine that I need this, we are running 7.4.

--


Best Regards,

Tarlika Elisabeth Schmitz


A: Because it breaks the logical sequence of discussion
Q: Why is top posting bad? 



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: extracting words
@ 2008-04-24 16:12  Tarlika Elisabeth Schmitz <postgresql@numerixtechnology.de>
  parent: A. Kretschmer <andreas.kretschmer@schollglas.com>
  0 siblings, 0 replies; 5+ messages in thread

From: Tarlika Elisabeth Schmitz @ 2008-04-24 16:12 UTC (permalink / raw)
  To: A. Kretschmer <andreas.kretschmer@schollglas.com>; +Cc: pgsql-sql

On Wed, 23 Apr 2008 07:18:44 +0200
"A. Kretschmer" <andreas.kretschmer@schollglas.com> wrote:

> am  Tue, dem 22.04.2008, um 22:25:04 +0100 mailte Tarlika Elisabeth
> Schmitz folgendes:
> > TABLE product: product_pk,  product_name, department_fk
> > TABLE product: department_pk,  department_name
> > 
> > ...
> > 
> > 
> > I need to extract the words from department_name and product_name
> > (words are separated by spaces) ...
> 
> 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)


Many thanks! This is ingenious!

Before I ask any stupid questions such as "what does replace (text, '
&', '') do?": where can I find a more detailed description of the
string functions?

I would like to treat all white space (new-line, tab, space) as
word separator.

Will the above work on 7.4?


--


Best Regards,

Tarlika Elisabeth Schmitz


A: Because it breaks the logical sequence of discussion
Q: Why is top posting bad? 



^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2008-04-24 16:12 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2008-04-22 21:25 extracting words Tarlika Elisabeth Schmitz <postgresql@numerixtechnology.de>
2008-04-23 05:18 ` A. Kretschmer <andreas.kretschmer@schollglas.com>
2008-04-24 16:12   ` Tarlika Elisabeth Schmitz <postgresql@numerixtechnology.de>
2008-04-23 13:13 ` Alvaro Herrera <alvherre@commandprompt.com>
2008-04-23 14:57   ` Tarlika Elisabeth Schmitz <postgresql@numerixtechnology.de>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox