No se pueden usar las funciones agregadas definidas por el usuario en la cláusula Oracle PIVOT.Funciones agregadas definidas por el usuario en Oracle PIVOT cláusula
Creé una función agregada definida por el usuario llamada string_agg.
soy capaz de utilizarlo en una declaración simple, como ...
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
Resultados en:
ID TYPE STRING_AGG(VALUE)
user1 CAR Audi,BMW
user1 COMPUTER Dell,Sony
user2 CAR Honda
user2 COMPUTER HP
Sin embargo cuando intento utilizar la misma función en una cláusula de pivote
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));
me sale el siguiente errores ...
ORA-56902: expect aggregate function inside pivot operation
esperado resultado es ...
TYPE USER1 USER2
COMPUTER Dell,Sony HP
CAR Audi,BMW Honda
"select id, string_agg (value) from table1" dará un error ORA-00937 ya que no tiene una cláusula group by. ¿Puede darnos un ejemplo completo de algunos sql con el pivote y su función de agregado falla? – Gerrat
Gracias por los comentarios. Publicación original editada para proporcionar un ejemplo detallado – nick
Parece que estás usando la función string_agg desde aquí: http://www.oracle-base.com/articles/misc/StringAggregationTechniques.php. No puedo encontrar ninguna razón para que esto falle. Tal vez un error (tal vez el pivote no funciona con las funciones agregadas definidas por el usuario)? Si cambia su segunda ocurrencia de string_agg a 'max', da los resultados que desea, aunque – Gerrat