I have two tables, A and B. In Table A, I have a column with machine names (Cortadeira, Baler, Packer and Rewinder) and in the other column some causes that made them stop Lack of Electric Power, Coil Replacement, etc).
In Table B , I have a column with the list of all possible reasons (In Table A there are only those that have already occurred, each row shows the name of one machine and the reason for your stop.)
Goal:
Have the code return me the number of times a particular machine has stopped for each reason, returning the value 0
if it has not been stopped by that.
Current Query:
SELECT
B.TodosOsMotivos,
A.MotivosQueAconteceram,
A.Maquinas,
COUNT(A.MotivosQueAconteceram) as NumeroDeOcorrencias
FROM A
RIGHT join B on A.MotivosQueAconteceram = B.TodosOsMotivos
WHERE A.Maquinas = 'Cortadeira'
OR A.Maquinas IS NULL
GROUP by MotivosQueAconteceram, Maquinas, B.TodosOsMotivos
order by B.TodosOsMotivos
That way she tells me how many times Cortadeira has stopped for a reason X and returns NULL
when no machine stopped for some reason. The problem is that when the LOCKER did not stop for a reason Y but another MACHINE stopped, it just does not return any value to me when I needed it to return 0/NULL
. What can I do to change this?
Tables:
This is a Table A pa_maquina
refers to the Question machines column and pa_motivos
refers to the MotivosQueAconteceram
column:
ThisisTableB,columndescricao
referstoTodosOsMotivos
: