获取按日期分组的列总和

时间:2018-07-17 11:55:19

标签: mysql sql

是否想知道是否有一种方法可以获取具有相同date_of_export的项目的stock_case列的总和?

此处更新了小提琴和一些相关数据:

https://www.db-fiddle.com/f/szC1Ftj3ZGEC24gSYp6ad4/4

预期输出为:

enter image description here 这是使用的查询

    SELECT 
    st.product_code,
    st.date_of_export,
    st.best_before_date,
    st.stock_case,
    (
    SELECT 
            SUM(st2.stock_case)
        FROM
            stock_tracking AS st2
            WHERE
            st2.product_code IN ('MGN003')
        AND MONTH(st2.date_of_export) IN (07)
        AND YEAR(st2.date_of_export) IN (2018)
        AND st2.stock_case != 0
    ) AS total
    FROM
    stock_tracking st
WHERE
    product_code IN ('MGN003')
        AND MONTH(st.date_of_export) IN (07)
        AND YEAR(st.date_of_export) IN (2018)
        AND stock_case != 0

和我的结果 enter image description here

希望有一个总计列,例如16,16,16,...,19等 在另一种情况下,我使用了像这样的子查询

SELECT 
    d.products_name,
    stock_case,
    st.date_of_export,
    st.best_before_date,
    st.product_code,
    (SELECT 
            SUM(st2.stock_case)
        FROM
            stock_tracking AS st2
        WHERE
            DATE(st2.date_of_export) = (SELECT 
                    DATE(tmp.last_update)
                FROM
                    (SELECT 
                        date_of_export AS last_update
                    FROM
                        stock_tracking
                    ORDER BY date_of_export DESC
                    LIMIT 1) AS tmp
                WHERE
                    product_code = 'MGN003')) AS total
FROM
    stock_tracking st
        LEFT JOIN
    products AS p ON p.products_model = st.product_code
        LEFT JOIN
    products_description AS d ON d.products_id = p.products_id
WHERE
    product_code = 'MGN003'
        AND d.language_id = 2
        AND DATE(st.date_of_export) = (SELECT 
            DATE(tmp.last_update)
        FROM
            (SELECT 
                date_of_export AS last_update
            FROM
                stock_tracking AS st
            ORDER BY date_of_export DESC
            LIMIT 1) AS tmp)

,结果如下: enter image description here

2 个答案:

答案 0 :(得分:1)

您可以先通过sum(stock_case)date_of_export写一个子查询,然后在self join上向Date写一个子查询,然后可以获得期望的结果。

SELECT  
    s.product_name,
    s.date_of_export,
    s.best_before_date,
    s.product_code,
    s.stock_case,
    t.totle
FROM
    stock_tracking s
INNER JOIN
    (
        SELECT SUM(stock_case) totle,date_of_export dt
       FROM stock_tracking
       where   
        product_code = 'MGN003'
        AND MONTH(date_of_export) =07
        AND YEAR(date_of_export) =2018
        AND stock_case != 0
      GROUP BY date_of_export
    ) t on DATE_FORMAT(s.date_of_export, "%d-%m-%Y") = DATE_FORMAT(t.dt, "%d-%m-%Y")
where 
    s.product_code = 'MGN003'
AND MONTH(s.date_of_export) =07
AND YEAR(s.date_of_export) =2018
AND s.stock_case != 0

sqlfiddle

答案 1 :(得分:-1)

没有给出确切答案:您应该朝以下方向思考:

SELECT SUM(column) FROM table WHERE ... GROUP BY date

SELECT SUM(column), DISTINCT date FROM table WHERE ...

因此查找GROUP BY和DISTINCT的工作方式:-)