Определяемые пользователем агрегатные функции в предложении Oracle PIVOT - PullRequest
5 голосов
/ 04 августа 2011

Невозможно использовать определяемые пользователем агрегатные функции в предложении Oracle PIVOT.

Я создал определяемую пользователем агрегатную функцию с именем string_agg.
Я могу использовать ее в простом выражении, например ...

select id, string_agg(value) from
(
    select 'user1' as id, 'BMW' as value,   'CAR'      as type from dual    union
    select 'user1' as id, 'Audi' as value,  'CAR'      as type from dual    union
    select 'user2' as id, 'Honda' as value, 'CAR'      as type from dual    union
    select 'user1' as id, 'Dell' as value,  'COMPUTER' as type from dual    union
    select 'user1' as id, 'Sony' as value,  'COMPUTER' as type from dual    union
    select 'user2' as id, 'HP' as value,    'COMPUTER' as type from dual 
)
group by id, type

Результат:
<b>ID          TYPE            STRING_AGG(VALUE)</b>
user1       CAR             Audi,BMW
user1       COMPUTER        Dell,Sony
user2       CAR             Honda
user2       COMPUTER        HP

Однако, когда я пытаюсь использовать ту же функцию в предложении сводки
select * from
( 
    select id, type, string_agg(value) as value from
    (
        select 'user1' as id, 'BMW' as value,   'CAR'      as type from dual    union
        select 'user1' as id, 'Audi' as value,  'CAR'      as type from dual    union
        select 'user2' as id, 'Honda' as value, 'CAR'      as type from dual    union
        select 'user1' as id, 'Dell' as value,  'COMPUTER' as type from dual    union
        select 'user1' as id, 'Sony' as value,  'COMPUTER' as type from dual    union
        select 'user2' as id, 'HP' as value,    'COMPUTER' as type from dual 
    )
    group by id, type
)
PIVOT (string_agg(value) FOR id IN ('user1' user1, 'user2' user2) );

, я получаю следующую ошибку ...
ORA-56902: expect aggregate function inside pivot operation<br>

Ожидаемый результат ... ... 1017 *
<b>TYPE        USER1       USER2</b>
COMPUTER    Dell,Sony   HP       
CAR         Audi,BMW    Honda

Ответы [ 2 ]

0 голосов
/ 04 августа 2011

А как насчет использования wmsys.wm_concat вместо пользовательского агрегата?

0 голосов
/ 04 августа 2011

Pivot не обязательно должен быть выше той же агрегатной функции:

select * from
( 
    select id, type, LISTAGG(value) WITHIN GROUP (ORDER BY 1) as value from
    (
        select 'user1' as id, 'BMW' as value,   'CAR'      as type from dual    union
        select 'user1' as id, 'Audi' as value,  'CAR'      as type from dual    union
        select 'user2' as id, 'Honda' as value, 'CAR'      as type from dual    union
        select 'user1' as id, 'Dell' as value,  'COMPUTER' as type from dual    union
        select 'user1' as id, 'Sony' as value,  'COMPUTER' as type from dual    union
        select 'user2' as id, 'HP' as value,    'COMPUTER' as type from dual 
    )
    group by id, type
)
PIVOT (max(value) FOR id IN ('user1' user1, 'user2' user2) );
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...