I have two tables:
table_enights
| id | descricao | quantidade |
| 1 | Maquina Prensa | 1 |
| 2 | Maquina Secadora | 2 |
table_bom_itens
| id | id_itens | codigo | quantidade |
| 1 | 1 | mp001 | 1 |
| 2 | 1 | mp002 | 2 |
| 3 | 1 | fx001 | 1 |
| 4 | 1 | fx002 | 5 |
| 5 | 2 | ms001 | 1 |
| 6 | 2 | ms002 | 2 |
| 7 | 2 | ms003 | 1 |
| 8 | 2 | ms004 | 1 |
| 9 | 2 | fx001 | 4 |
| 10 | 2 | fx002 | 1 |
I'm running the following query:
SELECT
SUM(tb.quantidade*quantidade)
FROM
tabela_bom_itens
GROUP BY
codigo;
What is to happen to tabela_itens
is the parent product, and in tabela_bom_itens
would be the children.
In tabela_bom_itens
would be the amount of parts that goes to each parent item.
So to know the amount of pieces that goes into the parent product was to multiply the amount of parent product for each child product, and if the same items appear in both, group and report the sum value of the items.
The expected result is:
| codigo | quantidade |
| mp001 | 1 |
| mp002 | 2 |
| ms001 | 2 |
| ms002 | 4 |
| ms003 | 2 |
| ms004 | 2 |
| fx001 | 9 |
| fx002 | 10 |
But what's coming back has nothing to do with what's wrong with my query?