0
SELECT EL_VALUES_FIELD1
     , EL_VALUES_FIELD2
     , EL_VALUES_FIELD4
     , SUM (EL_VALUES_FIELD5) 
FROM DATA_FORM_VALUES_919906 
GROUP 
    BY EL_VALUES_FIELD1
     , EL_VALUES_FIELD2
     , EL_VALUES_FIELD4

Image 1 is the source table and Image 2 is the destination image

source

destination

Strawberry
  • 33,750
  • 13
  • 40
  • 57

2 Answers2

0

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;
Harshil Doshi
  • 3,497
  • 3
  • 14
  • 37
0

If you're using MySQL, use GROUP_CONCAT to combine the values from field3.

INSERT INTO destination (keyfield1, keyfield2, keyfield3, keyfield4, amount)
SELECT field1, field2, GROUP_CONCAT(field3), field4, SUM(amount)
FROM source
GROUP BY field1, field2, field4

See Is there any function in oracle similar to group_concat in mysql? if you're actually using Oracle.

Barmar
  • 741,623
  • 53
  • 500
  • 612