sql >> Base de Datos >  >> RDS >> Mysql

Cómo sumar con condiciones en otras columnas

Use esta consulta a continuación:-

 CREATE TEMPORARY TABLE t1 (PONumber decimal(18, 2) NOT NULL,POdate date NOT NULL,customername varchar(200) NOT NULL,description varchar(200) NOT NULL);
 INSERT INTO t1(PONumber,POdate,customername,description) 
 select distinct PONumber,POdate,customername,description from tb_po;

 select  p.PONumber,p.podate,p.customername,p.description,
(select sum(q.deliveryqty) from tb_spb q where p.PONumber=q.PONumber AND p.description = q.description) as Total
 from t1 p;

drop table t1;

Le dará la salida requerida.