Ver Mensaje Individual
  #11 (permalink)  
Antiguo 27/07/2010, 10:36
Avatar de huesos52
huesos52
Colaborador
 
Fecha de Ingreso: febrero-2009
Ubicación: Manizales - Colombia
Mensajes: 5.980
Antigüedad: 15 años, 3 meses
Puntos: 360
Respuesta: uso de left join y sum

Definitivamente la solución son subconsultas.

A mi se me ocurrió algo similar


Código SQL:
Ver original
  1. SELECT
  2. t.estado,
  3. SUM(COALESCE(t.valor,0)) AS suma_definitiva
  4. FROM
  5. (
  6.  SELECT  
  7.     e.est_002 AS estado,
  8.     e.reg_001,
  9.     e.est_001,
  10.     a.agri04_001 AS ano,
  11.     a.agri04_005 AS valor
  12.  FROM estado e
  13.  LEFT JOIN
  14.  agri04 a
  15.  ON
  16.  (e.reg_001,e.est_001)=(a.reg_001,a.est_001)
  17. ) AS t
  18. WHERE t.ano = '2005'
  19. GROUP BY t.estado;
EDITO
o así
Código SQL:
Ver original
  1. SELECT
  2. t.estado,
  3. SUM(COALESCE(t.valor,0)) AS suma_definitiva
  4. FROM
  5. (
  6.  SELECT  
  7.     e.est_002 AS estado,
  8.     e.reg_001,
  9.     e.est_001,
  10.     a.agri04_001 AS ano,
  11.     a.agri04_005 AS valor
  12.  FROM estado e
  13.  LEFT JOIN
  14.  agri04 a
  15.  ON
  16.  (e.reg_001,e.est_001)=(a.reg_001,a.est_001)
  17. ) AS t
  18. GROUP BY t.estado
  19. HAVING t.ano = '2005';

No tengo como probarla.
__________________
Without data, You are another person with an opinion.
W. Edwads Deming

Última edición por huesos52; 27/07/2010 a las 10:46