我需要帮助来总结代码相同的每个值,这是我的数据
OFFICECODE | DATE | VAL
10200 | 2018-05-01 | 1
10200 | 2018-05-02 | 2
10100 | 2018-05-01 | 3
10100 | 2018-05-02 | 2
我想得到这个结果
OFFICECODE | DATE | SUM(VAL)
10200 | 2018-05-01 | 1
10200 | 2018-05-02 | 3 (1+2)
10100 | 2018-05-01 | 3
10100 | 2018-05-02 | 5 (3+2)
我尝试此查询,但结果总和所有OFFICECODE
SELECT
A.OFFICELKD,
A.DATE,
A.VAL,
@VAL := @VAL + A.VAL AS `VALSUM`
FROM
( SELECT
cur.OFFICELKD,
cur.DATE,
cur.VAL
FROM
tblReport cur
LEFT JOIN
tblReport pre
ON cur.OFFICECODE = pre.OFFICECODE
GROUP BY cur.ID
ORDER BY
OFFICECODE ) AS A,
( SELECT @VAL := 0 ) AS SqlA
ORDER BY OFFICECODE, DATE
请帮我解决这个问题
答案 0 :(得分:1)
你很亲密。您需要考虑OFFICECODE
:
SELECT r.*,
(@val := if(@o = r.OFFICECODE, @val + a.val,
if(@o := r.OFFICECODE, a.val, a.val)
)
) as valsum
FROM (SELECT r.*
FROM tblReport r
ORDER BY OFFICECODE, DATE
) r CROSS JOIN
( SELECT @o := -1, @VAL := 0 ) params;