sql >> Base de Datos >  >> RDS >> Oracle

Obtener la suma de varias columnas de dos tablas

Puede unir sus tablas antes del grupo (esto está en Oracle, por cierto):

SELECT t.month_ref, SUM(t.amount1), SUM(t.amount2)
  FROM (SELECT month_ref, amount1, amount2
          FROM T_FOO
         WHERE seller = XXX
         UNION ALL
        SELECT month_ref, amount1, amount2
          FROM T_BAR
         WHERE seller = XXX
         ) t
 GROUP BY t.month_ref

También puede unir las tablas con el campo del vendedor y filtrarlo más tarde (en caso de que necesite una lógica más avanzada):

 SELECT t.month_ref, SUM(t.amount1), SUM(t.amount2)
   FROM (SELECT month_ref, amount1, amount2, seller
           FROM T_FOO
          UNION ALL
         SELECT month_ref, amount1, amount2, seller
           FROM T_BAR) t
  where t.seller = XXX
  GROUP BY t.month_ref