<< Click to Display Table of Contents >> Aggregates |
![]() ![]() ![]() |
create table tabela (codigo int, valor char(1))
insert into tabela values (1, 'A'), (1, 'B'), (1, 'C'), (2, 'A'), (2, 'B'), (3, 'X'), (3, 'Y'), (3, 'Z')
select * from tabela
codigo valor
1 A
1 B
1 C
2 A
2 B
3 X
3 Y
3 Z
Mágica
create function temp1(text, text)
returns text AS
'select case
when $2 is null or $2 = '''' then $1
when $1 is null or $1 = '''' then $2
else $1 || '', '' || $2
end'
language sql;
create aggregate ag_temp(basetype = text, sfunc = temp1, stype = text, initcond = '');
select codigo, ag_temp(valor) from tabela group by codigo
Result
codigo ag_temp
1 A, B, C
3 X, Y, Z
2 A, B