如何获取mysql中另一列中所有记录的总和

时间:2015-03-05 18:34:03

标签: mysql join

我希望显示另一列中特定列的所有总和而不更改原始数量。以下是我的表格。

我的原始batch_request表

   +----------+-------------+
    | order_id | Ordertype |
    +----------+------------+
    | 31234567 |         A1 |
    | 5xyzsder |         A1 |
    | 8DQVIHSC |         A1 | 
    | xyz123   |         B2 |
    | t123     |         B2 |
    +----------+-------------+

原始reports_ref表

+----------+---------------+---------------+
| order_id | SUBMITTED_QTY | STOCK_ID      |
+----------+---------------+---------------+
| 31234567 |          3.00 | 4032611179505 |
| 5xyzsder |          3.00 | 4032611179505 |
| 8DQVIHSC |          4.00 | 4032611179505 |
| xyz123   |          5.00 | 4032611179505 |
| t123     |          15.00| 4032611179505 |  
+----------+---------------+---------------+

我的查询内容

+----------+---------------+---------------+
| order_id | SUBMITTED_QTY | STOCK_ID      |
+----------+---------------+---------------+
| 31234567 |          3.00 | 4032611179505 |
| 5xyzsder |          3.00 | 4032611179505 |
| 8DQVIHSC |          4.00 | 4032611179505 |
+----------+---------------+---------------+

我想要的输出将是这样的。

    +----------+---------------+---------------+------+
    | order_id | SUBMITTED_QTY | STOCK_ID      |Total |
    +----------+---------------+---------------+------+
    | 31234567 |          3.00 | 4032611179505 |10.00 |
    | 5xyzsder |          3.00 | 4032611179505 |10.00 |
    | 8DQVIHSC |          4.00 | 4032611179505 |10.00 |
    +----------+---------------+---------------+------+
   For this  my query is 

SELECT ref.order_id,SUM(ref.SUBMITTED_QTY) SUBMITTED_QTY, ref.STOCK_ID 
        FROM batch_request bo 
        JOIN reports_ref ref ON bo.order_id = ref.order_id WHERE ref.STOCK_ID = '4032611179505'

但是我得到了这个。如何实现我想要的输出?

    +----------+---------------+---------------+
    | order_id | SUBMITTED_QTY | STOCK_ID      |
    +----------+---------------+---------------+
    | 31234567 |          10.00| 4032611179505 |
    +----------+---------------+---------------+

如何实现我想要的输出?请问我在哪里做错了?

这是原始查询

    SELECT ref.order_id,
       SUM(ref.SUBMITTED_QTY) SUBMITTED_QTY, 
       ref.STOCK_ID 
    FROM batch_request bo 
    JOIN reports_ref ref 
    ON bo.order_id = ref.order_id 
    WHERE ref.STOCK_ID = '4032611179505' 
      AND bo.order_type = 'A1' 
      AND DATE(ref.inserted_on) BETWEEN '2015-01-01' AND '2015-03-02'

2 个答案:

答案 0 :(得分:1)

SELECT ref.order_id,ref.SUBMITTED_QTY, ref.STOCK_ID ,
    t.total
FROM reports_ref ref
INNER JOIN (
  SELECT DISTINCT order_id
  FROM batch_request
  WHERE order_type = 'A1' 
) as bo  
ON bo.order_id = ref.order_id 
LEFT JOIN (
    SELECT 
       STOCK_ID,
       SUM(SUBMITTED_QTY) as total
    FROM reports_ref
    WHERE STOCK_ID = '4032611179505'
    GROUP BY STOCK_ID
) AS t
ON t.STOCK_ID = ref.STOCK_ID
WHERE ref.STOCK_ID = '4032611179505'
  AND DATE(ref.inserted_on) BETWEEN '2015-01-01' AND '2015-03-02'

答案 1 :(得分:0)

试试这个:

SELECT ref.order_id,ref.SUBMITTED_QTY,ref.STOCK_ID,SUM(ref.SUBMITTED_QTY)as Total FROM reports_ref as ref JOIN batch_requests as bo ON bo.order_id = ref.order_id WHERE ref.STOCK_ID ='4032611179505'; < / p>