Following query will get the job done:
select field1,
field2,
Group_concat(field3 order by field3 desc separator ',') as keyfield_3,
field4,
sum(amount) as amount
from table
group by field1,field2,field4;
Above query will only work for MySQL and only display result. If you want to insert these values in another table then do the following:
Insert into tablename(keyfield1,keyfield2,keyfield3,keyfield4,amount)
select field1,
field2,
Group_concat(field3 order by field3 desc separator ',') as keyfield_3,
field4,
sum(amount) as amount
from table
group by field1,field2,field4;
If you want to achieve same task in Oracle, you should use LISTAGG()
instead of Group_concat()
Oracle:
Insert into tablename(keyfield1,keyfield2,keyfield3,keyfield4,amount)
select field1,
field2,
LISTAGG(field3,',') WITHIN GROUP (order by field3 desc),
field4,
sum(amount) as amount
from table
group by field1,field2,field4;