Re: Using crosstab in tablefunc

Поиск
Список
Период
Сортировка
От Joe Conway
Тема Re: Using crosstab in tablefunc
Дата
Msg-id 3E1DC9C8.3010208@joeconway.com
обсуждение исходный текст
Ответ на Using crosstab in tablefunc  (Adam Witney <awitney@sghms.ac.uk>)
Список pgsql-general
Adam Witney wrote:
>  biomaterial_id | category |    value
> ----------------+----------+--------------
>              32 | genotype | CQ sensitive
>              32 | species  | P.falciparum
>              32 | strain   | 3D7

[...snip...]

>
> There are 3 categories..... When I use crosstab I get this
>
> test=# select * from crosstab('select a.biomaterial_id, category, value from
> v_biosource_writeable a, v_characteristics b where a.biomaterial_id =
> b.biomaterial_id and (category = ''species'' or category = ''strain'' or
> category = ''genotype'') order by 1,2;', 3) as ct(biomaterial_id int,
> species text, strain text, genotype text);

You asked for the categories to be named incorrectly, I think. If the
categories are genotype, species, and strain in that order, then just name the
crosstab columns as such:

select *
from crosstab(
   'select a.biomaterial_id, category, value
    from v_biosource_writeable a, v_characteristics b
    where a.biomaterial_id = b.biomaterial_id
    and (category = ''species'' or category = ''strain''
         or category = ''genotype'')
    order by 1,2'
    , 3)
AS ct(biomaterial_id int, genotype text, species text, strain text);
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

I.e. the categories will be in the order provided by the query submitted to
crosstab. The names you give the categories in the AS column reference clause
are completely arbitrary. You could just as easily substitute:
   AS ct(f1 int, c1 text, c2 text, c3 text);

HTH,

Joe


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

Предыдущее
От: Thomas O'Connell
Дата:
Сообщение: Re: Question about DEADLOCK
Следующее
От: "Andy Kriger"
Дата:
Сообщение: Re: Running PostgreSQL on Windows