Re: [GENERAL] Howto convert arrays 2 query results

From: Herouth Maoz <herouth(at)oumail(dot)openu(dot)ac(dot)il>
To: Jeroen Schaap <jeroen(at)rulffh(dot)medfac(dot)leidenuniv(dot)nl>, pgsql-general(at)postgreSQL(dot)org
Subject: Re: [GENERAL] Howto convert arrays 2 query results
Date: 1999-06-09 15:05:44
Message-ID: l03130301b384306b7c2a@[147.233.159.109]
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

At 10:13 +0300 on 09/06/1999, Jeroen Schaap wrote:

> Do you know of any way to generally convert arrays into query results?
>
> I know it is better to implement arrays as tables, but that results in
> unreadable tables (with 10 rows with id=1, 15 with id=2, 2 with id=3 ad
> infundum...).
>
> So is there any way to convert an array into a table? Should I
> write a function or a C-function?

It's not entirely clear what you want. The reason to keep arrays together
in a separate table is organizational. The way you want to present the
arrays shoud not affect the way they are organized.

If it bothers you that a query returns something like

id person child
=== ======= =======
1 Susan Tom
1 Susan Merry
1 Susan Donna
2 George Ben
2 George Peggy
3 Morris Elias

And you want it to show something like:

Person Children
====== ========
Susan Tom, Merry, Donna
George Ben, Peggy
Morris Elias

What you do is write it this way in the frontend. It depends on your
favourite frontend language, but the general algorithm should be something
along the lines of:

last_id = 0;

while ( still_more_tuples )

get_next_tuple;

if ( tuple.id = last_id )
print( "," + tuple.child )
else
print( <newline> + tuple.person + <tab> + tuple.child )
end if

last_id = tuple.id;

end while

Herouth

--
Herouth Maoz, Internet developer.
Open University of Israel - Telem project
http://telem.openu.ac.il/~herutma

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Adriaan Joubert 1999-06-09 15:47:39 User-defined types and indices
Previous Message Jeroen Schaap 1999-06-09 07:13:05 Howto convert arrays 2 query results