Re: [SQL] counting related rows

2010-10-15 Thread James Cloos
After further though, I tried using a function:

CREATE OR REPLACE FUNCTION children ( ow integer, parent text) returns integer 
AS $$
select count(*) as children from m where o = $1 and name ilike $2 || '/%';
$$
LANGUAGE sql;

An example call is:

select o, name, f1, f2, (select children(o,name) as children) from m where o=3;

Which worked, but was no faster than selecting all of the matching ids
and iterating through them on the client finding each row's parent count
each in its own select.  Ie, it took about 1 ks for about 20k rows.

So it looks like the real solution is to add a column to track the
number of children and update it, for each "parent" row via a trigger
whenever a row is added, removed or the path column of a row is changed.

-JimC
-- 
James Cloos  OpenPGP: 1024D/ED7DAEA6

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


[SQL] How to collect text-fields from multiple rows ?

2010-10-15 Thread Andreas

 Hi,

how can I collect text-fields from multiple rows into one output row?
I'd like to do an equivalent to the aggregate function SUM() only for text.

The input is a select that shows among other things a numerical column 
where I would like to group by.
The text column of all rows in a group should get concatenated into 1 
text devided by a '\n'.


Even better would be if I could add a second text colum per line as topic.

Input e.g.
select  group_nr::integer, memo::text, topic::text ...

1, 'bla ', 'weather'
2, 'yada..', 'weather'
2, 'talk talk..', 'cooking'
2, 'words words, ...', 'poetry'
3, 

Output:

1, 'weather\nbla...'
2, 'weather\nyada..\ncooking\ntalk talk..\npoetry\nwords words, ...'


Even better would be to get some kind of line-chars as optical devider 
between the topics.

Lets say 10 = above all topic-lines.

2, '==\nweather\nyada..\n==\ncooking\ntalk 
talk..\n==\npoetry\nwords words, ...'





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