Query the database with merging records

Hello

In the database the data is stored as a table:
2

How to create a query in Wappler, so that the output is formed according to the condition - if the records have the same names field 1 and field 2, the records are combined into one, and the values of the fields with the number of units are added. It looks like this:
3

Strongly do not scold. So far I am weak in working with the database.

Thanks in advance for help.

{{field1 == field2 ? field1+field2 : field1}} client side condition

mysql

IF(condition, expression_if_true, expression_if_false)

field1 == field2 is not the condition I wrote about in the first message.

I need a condition when the records have the same values both on field1 and on field2, then the records merge and the numeric values add up, as shown in the screenshot.

And I would really appreciate it if you would describe the process in more detail what I need to do in Wappler step by step.

Thanks a lot!

So you want to group by field 1 and field 2. A custom query would look like:

SELECT field1, field2, SUM(field3) FROM table GROUP BY field1, field2

You can do this with the custom query or with the query builder, just use the aggregate function SUM on the field3 and it will automaticly group by the other fields you select in the output.

2 Likes

Hello, @patrick

I apologize that did not answer at once, only now reached implementation of this query. I come a little problem: I made a query using the query Builder, following your instructions. The query combines the rows exactly as I need, but for some reason the field in which the total number (SUM) should be displayed is empty. What can it be related to and how to fix it?

Thank you in advance for your help!

Did you give the SUM column an alias so that you can access it by that name?

1 Like

No, I forgot to do it… now everything works perfectly!))

Thank you very much for your help!