> Just realised that's not what you're after, but my first point still
> stands.
Thank you.
I tried to wrap words at 15 characters using code below.
Issues:
1. Table rows places same word to multiple lines. How to remove them so that
every word appears only in single row?
2. In last select sum(word||' ') causes error. How to concatenate words
bact to row (inverse of unnest() function ?
Andrus.
create temp table words( id serial, word text ) on commit drop;
insert into words (word) select * from unnest(string_to_array('Quick brown
fox runs in forest.',' '));
create temp table results on commit drop as
select
first.id as first,
last.id as last,
sum(length(a.word)+1) as charcount
from words a, words first, words last
where a.id between first.id and last.id
group by 1,2
having sum(length(a.word)+1)<15;
create temp table maxr on commit drop as
select
first,
max(charcount) as charcount
from results
group by 1;
create temp table rows on commit drop as
select
first,
last
from results
join maxr using (first,charcount)
order by 1;
select
rows.first,
sum(word||' ')
from rows, words
where words.id between first and last
group by 1
order by 1, words.id