Foros del Web » Programación para mayores de 30 ;) » Bases de Datos General »

counts que siempre sale un +1 de mas

Estas en el tema de counts que siempre sale un +1 de mas en el foro de Bases de Datos General en Foros del Web. Alguien sabe cual es la explicacion logica a esto: Tengo este select: @import url("http://static.forosdelweb.com/clientscript/vbulletin_css/geshi.css"); Código SQL: Ver original SELECT Entrega . Referencia , COUNT ( ...
  #1 (permalink)  
Antiguo 15/11/2015, 08:50
 
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
  #2 (permalink)  
Antiguo 16/11/2015, 09:44
Avatar de Libras
Colaborador
 
Fecha de Ingreso: agosto-2006
Ubicación: En la hermosa perla de occidente
Mensajes: 7.412
Antigüedad: 17 años, 8 meses
Puntos: 774
Respuesta: counts que siempre sale un +1 de mas

sin el left join te hace un producto cartesiano, une las 2 tablas y saca los que tienen coincidencia, con el left join nada mas te retorna lo que esta en la segunda tabla
__________________
What does an execution plan say to t-sql query? Go f**k yourself, if you are not happy with me

Etiquetas: registro, select, siempre
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 05:16.