Ver Mensaje Individual
  #1 (permalink)  
Antiguo 15/11/2015, 08:50
sergioo
 
Fecha de Ingreso: abril-2008
Mensajes: 41
Antigüedad: 16 años
Puntos: 0
counts que siempre sale un +1 de mas

Alguien sabe cual es la explicacion logica a esto:
Tengo este select:
Código SQL:
Ver original
  1. SELECT Entrega.Referencia, COUNT(*) AS 'Entregadas'
  2. FROM (SELECT TEF.nombre AS Referencia
  3.       FROM      pieza_datamatrix AS PDM
  4.         [B]LEFT JOIN[/B] det_contenedores AS DC ON DC.N_Serie=PDM.id
  5.                 LEFT JOIN tipo_elemento_fabricacion AS TEF ON TEF.id=PDM.idTipo
  6.       GROUP BY TEF.id, DC.N_Serie) AS Entrega
  7. GROUP BY Entrega.Referencia
  8.  
  9. UNION
  10.  
  11. SELECT Entrega.Referencia, COUNT(*) AS 'Entregadas'
  12. FROM (SELECT TEF.nombre AS Referencia
  13.       FROM      ff_pieza_datamatrix AS PDM2
  14.                   [B]LEFT JOIN[/B] ff_det_contenedores AS DC2 ON DC2.N_Serie=PDM2.id
  15.           LEFT JOIN tipo_elemento_fabricacion AS TEF ON                TEF.id=PDM2.idTipo
  16.       GROUP BY TEF.id, DC2.N_Serie) AS Entrega
  17. GROUP BY Entrega.Referencia

el resultado es:
Cita:
00675283 1
04123158 33
04123412 37
04132010 1474
04133081 1
04133947 5
04133982 1
Sin embargo si hago la consulta quitando el primer left join de las consultas, tal que asi:
Código SQL:
Ver original
  1. SELECT Entrega.Referencia, COUNT(*) AS 'Entregadas'
  2. FROM (SELECT TEF.nombre AS Referencia
  3.       FROM      pieza_datamatrix AS PDM
  4.         [B]JOIN [/B]det_contenedores AS DC ON DC.N_Serie=PDM.id
  5.                 LEFT JOIN tipo_elemento_fabricacion AS TEF ON TEF.id=PDM.idTipo
  6.       GROUP BY TEF.id, DC.N_Serie) AS Entrega
  7. GROUP BY Entrega.Referencia
  8.  
  9. UNION
  10.  
  11. SELECT Entrega.Referencia, COUNT(*) AS 'Entregadas'
  12. FROM (SELECT TEF.nombre AS Referencia
  13.       FROM      ff_pieza_datamatrix AS PDM2
  14.                   [B]JOIN [/B]ff_det_contenedores AS DC2 ON DC2.N_Serie=PDM2.id
  15.           LEFT JOIN tipo_elemento_fabricacion AS TEF ON                TEF.id=PDM2.idTipo
  16.       GROUP BY TEF.id, DC2.N_Serie) AS Entrega
  17. GROUP BY Entrega.Referencia

el resultado es correcto y sale asi:
Código:
04123158	32
04123412	36
04132010	1473
04133947	5
04293426	2323
04510155	16
Con la primera consulta CASI SIEMPRE salen todos los resultados con un +1 (incluyendo los registros que no deberian salir que salen con un =1)
porque siempre saca un +1 mas al valor que debería??

Última edición por gnzsoloyo; 16/11/2015 a las 09:45