I have a stock movement table with the following columns and data:
table moves
id | operacao | prodinsumo | qtde
----------------------------------
1 | 1 | 25 | 6
2 | 2 | 10 | 3
3 | 10 | 17 | 1
4 | 11 | 10 | 2
table supplies :
codinsumo | nomeinsumo
------------------------
10 | água mineral
table products :
codproduto | nomeproduto
---------------------------
25 | refrigerante
17 | espetinho
In the column operacao
are stored the codes of operations that are:
1 - Sell Product
2 - Sale Input
10 - Product Reversal
11 - Reversal Input
The column prodinsumo
stores the input or product code according to the operation.
I would like to be able to do a query that puts the name of the input or the product according to the operation, but I do not know how to do it.
Here is a query that I used to select the columns, but it does not decide which table will get the name, if it is that of the inputs or products.
select
case m.operacao
when 1 then "Venda produto"
when 2 then "Venda insumo"
when 10 then "Estorno produto"
when 11 then "Estorno insumo"
end as operacao,
i.nomeinsumo as nome, -- ou p.nomeproduto as nome
m.qtde
from movimentacoes m
left join produtos p on m.prodinsumo = p.codproduto
left join insumos i on m.prodinsumo = i.codinsumo
I need that when the column operacao
is with the values 1
or 10
the column nome
is with the value of column nomeproduto
of table produtos
and when it has the values 2
% or 11
is with the value of column nomeinsumo
of table insumos
.