Re: self referencing table.

Поиск
Список
Период
Сортировка
От David Johnston
Тема Re: self referencing table.
Дата
Msg-id 6E4E50EA-62DD-43DE-B36F-FE4A1D507382@yahoo.com
обсуждение исходный текст
Ответ на self referencing table.  (David Salisbury <salisbury@globe.gov>)
Список pgsql-general

On Jan 17, 2012, at 19:31, David Salisbury <salisbury@globe.gov> wrote:

>
> I've got a table:
>
>               Taxa
>     Column     |            Type
> ----------------+-----------------------------
> id              | integer                     |
> parent_id       | integer                     |
> taxonomic_rank  | character varying(32)       |
> latin_name      | character varying(32)
>
> It's basically a self referential table, with
> values in the taxonomic_rank like
>
> phylum
> family
> order
> genus
> species
>
> So at any row in the table I can get all the parent
> information be traversing upward using the parent id.
>
> However I'm interested in only getting just genus and species
> when I'm given a taxa.id value.  It would be a nice simple
> self join if the taxa.id I was given was always to a
> row with rank of 'species'.  Problem is, grasses don't
> have species, so sometimes my id is pointing to a genus
> row instead ( the id will be to lowest rank  ), so the
> parent is of no use.
>
> I'm thinking that I'm going to have to resort to a plpgsql
> function to solve this, but I'm hoping for a straight sql
> query that can return ...
>
> genus, species ( if it's a grass id the species value will be null )
>
> ... for any given taxa.id without iterating in a function.
>
> Actually, I was hoping to join this taxa table with it's
> referencing table and pull a view with the genus/species
> added in.
>
> possible?  Thanks for any ideas,
>
>

8.4 or better you should be able to use WITH RECURSIVE.

David J.

В списке pgsql-general по дате отправления:

Предыдущее
От: David Salisbury
Дата:
Сообщение: self referencing table.
Следующее
От: Fujii Masao
Дата:
Сообщение: Re: [ADMIN] PG synchronous replication and unresponsive slave