如何与其他列的条件求和

时间:2016-05-18 08:00:08

标签: mysql sql

我试图在其他表上使用po编号对deliveryqty求和,

SELECT
  po.PONumber,
  po.PODate,
  po.customername,
  po.Description,
  SUM(spb.DeliveryQty)
FROM
  tb_po AS po
LEFT OUTER JOIN
  tb_spb AS spb ON po.PONumber = spb.PONumber
GROUP BY
  po.PONumber,
  po.Description

和mysql显示这样的数据

PONUMBER   podate      customername    description          deliveryqty
4600293473 04/04/2016 Balikpapan-CLUB AMDK CLUB 1500 ML      810
4600293473 04/04/2016 Balikpapan-CLUB AMDK CLUB 600 ML       810
4600293473 04/04/2016 Balikpapan-CLUB AMDK CLUB GALON 19 LT  810
4600293487 04/04/2016 Balikpapan-CLUB AMDK CLUB GALON 19 LT  800
4600293489 04/04/2016 Balikpapan-CLUB AMDK CLUB 240 ML      1200
4600293489 04/04/2016 Balikpapan-CLUB AMDK CLUB 330 ML      1200

我尝试的是根据宝数和数据求和数据。材料描述并没有将所有数量总结为像这样的po数:

PONUMBER   podate      customername    description          deliveryqty
    4600293473 04/04/2016 Balikpapan-CLUB AMDK CLUB 1500 ML      250
    4600293473 04/04/2016 Balikpapan-CLUB AMDK CLUB 600 ML       440
    4600293473 04/04/2016 Balikpapan-CLUB AMDK CLUB GALON 19 LT  120
    4600293487 04/04/2016 Balikpapan-CLUB AMDK CLUB GALON 19 LT  800
    4600293489 04/04/2016 Balikpapan-CLUB AMDK CLUB 240 ML      1200
    4600293489 04/04/2016 Balikpapan-CLUB AMDK CLUB 330 ML         0

这是我用的表

tb_po

PONUMBER   podate      customername    description          
4600293473 04/04/2016 Balikpapan-CLUB AMDK CLUB 1500 ML      
4600293473 04/04/2016 Balikpapan-CLUB AMDK CLUB 600 ML       
4600293473 04/04/2016 Balikpapan-CLUB AMDK CLUB GALON 19 LT
4600293487 04/04/2016 Balikpapan-CLUB AMDK CLUB GALON 19 LT 
4600293489 04/04/2016 Balikpapan-CLUB AMDK CLUB 240 ML    
4600293489 04/04/2016 Balikpapan-CLUB AMDK CLUB 330 ML      

tb_spb

  PONUMBER    podate     customername    description          deliveryqty
    4600293473 04/04/2016 Balikpapan-CLUB AMDK CLUB 1500 ML      125
    4600293473 04/04/2016 Balikpapan-CLUB AMDK CLUB 1500 ML      125
    4600293473 04/04/2016 Balikpapan-CLUB AMDK CLUB 600 ML       440
    4600293473 04/04/2016 Balikpapan-CLUB AMDK CLUB GALON 19 LT  120
    4600293487 04/04/2016 Balikpapan-CLUB AMDK CLUB GALON 19 LT  400
    4600293487 04/04/2016 Balikpapan-CLUB AMDK CLUB GALON 19 LT  400
    4600293489 04/04/2016 Balikpapan-CLUB AMDK CLUB 240 ML      1200
    4600293489 04/04/2016 Balikpapan-CLUB AMDK CLUB 240 ML      1200
    4600293489 04/04/2016 Balikpapan-CLUB AMDK CLUB 240 ML      1200
    4600293489 04/04/2016 Balikpapan-CLUB AMDK CLUB 330 ML         0

1 个答案:

答案 0 :(得分:1)

使用以下查询: -

 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;

它将为您提供所需的输出。