agrupa por tecnico também.
Em 17 de setembro de 2015 17:03, Adam Lincoln <adamlinc@gmail.com> escreveu:
Galera, preciso de uma ajuda aqui,--
Estou com uma consulta SQL que faz join em duas tabelas.
Na tabela_1 eu tenho um unico registo de cada.
Na tabela_2 eu tenho varios registros com numero igual, porem o restante diferente.
Eu preciso buscar todos os registros na linha onde a DATA_INICIAL é a maior. Onde no fim ele me traga todos os registros na forma que estão na linha com data maio sem se repetir.
Numero Tecnico Data_incial
1234 Joao 01/09/2015 03:34
1234 null 02/09/2015 10:34
1234 Pedro 03/09/2015 14:20
1234 Marcos 05/09/2015 08:30
9999 Vitor 13/07/2015 10:26
9999 null 03/08/2015 11:10
9999 Jose 03/08/2015 15:44
9999 Luiz 06/08/2015 10:44
Gostaria de ter:
Numero Tecnico Data_incial
1234 Marcos 05/09/2015 08:30
9999 Luiz 06/08/2015 10:44
O select abaixo funciona, ele me traz os registros onde a DATA_INICIAL é a maior, MAS o campo TECNICO vem sempre o primeiro da lista e não onde a DATA_INICIAL é a maior.
$consulta = mysql_query("SELECT
MAX(tabela_2.DT_HORA_INICIAL) as DT_HORA_INICIAL,
tabela_1.NUMERO,
tabela_1.AGENTE,
tabela_2.NUMERO,
tabela_1.DATA,
carga_chamados.NUMERO,
tabela_2.TECNICO,
SUM(tabela_2.TMP_IMPACTO) as TMP_IMPACTO,
tabela_2.STATUS,
tabela_1.DT_HORA_ABERTURA
FROM metas.tabela_1 tabela_1
INNER JOIN metas.tabela_2 tabela_2 ON (tabela_2.NUMERO = tabela_1.NUMERO)
LEFT JOIN metas.carga_chamados carga_chamados ON (carga_chamados.NUMERO = tabela_1.NUMERO)
WHERE (`tabela_1`.`DATA` = '$data') and tabela_2.DT_HORA_INICIAL = DT_HORA_INICIAL
GROUP BY tabela_2.NUMERO
");
You received this message because you are subscribed to the Google Groups "Android Brasil - Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to androidbrasil-dev+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
You received this message because you are subscribed to the Google Groups "Android Brasil - Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to androidbrasil-dev+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.






0 comentários:
Postar um comentário