1
I would like to make a query in two tables that takes into account 3 criteria, but is not presenting the expected result.
Select viaturas.vtr_numero
From viaturas, despacho_ocorrencias
Where
viaturas.parado_manutencao='NÃO' and
viaturas.parado_utilizacao='NÃO' and
despacho_ocorrencias.fechar_despacho='NÃO'
group by viaturas.vtr_numero
In this query above I would like to show only the number of vehicles that had no maintenance, that had no use stops and with open dispatch (in this case with flag 'YES')but the last condition is not working because the ones with flag 'YES' and 'NO'.
Is there something wrong in the query? if I do the first two conditions is normal, put together the last condition the result does not match.
can show how are the data in the table? it was not very clear
– Ricardo Pontual
Marcelo would be the ideal you put the table structure and an example of the expected output, so it would be easier to help you
– Killdary Aguiar de Santana
Xará, what is the relationship between the tables
viaturas
anddespacho_ocorrencias
? You’re making a Cartesian product from both tables.– Marcelo Shiniti Uchimura
as @Marceloshinitiuchimura commented, is using two tables without establishing the relationship between them (with
join
or in thewhere
), then do the cartesiado, combining the results– Ricardo Pontual
I do not understand why this clause GROUP BY if you do not use any aggregation function. Assuming that when you say "with open dispatch (in this case with flag 'YES')" you are referring to another field other than the dispatch_occurrences.close_dispatch specified in the query then you would only have to replace it with the correct field. Note that you are making a CROSS JOIN, or Cartesian product of the vehicle tables and dispatch_occurrences.
– anonimo
By the comments are missing link the two tables, I will make test.
– Marcelo C. França