如何为查询中的每个组选择运行总计

时间:2014-09-10 09:10:58

标签: mysql running-total

SELECT  district_lease, 
        first_proddate, 
        prod_cnt, 
        @rt := @rt + prod_cnt AS api_cnt        
    FROM Temp1 a
    JOIN (SELECT @rt := 0) r
    GROUP BY district_lease, first_proddate
    ORDER BY district_lease, first_proddate ; 

此查询不是通过group_lease,first_proddate组计算运行总计,而是计算完整查询的运行总计。我们想要的是每个district_lease和first_proddate的运行总计。

1 个答案:

答案 0 :(得分:0)

使用sum()功能

查询:

SELECT  district_lease, 
        first_proddate, 
        prod_cnt, 
        sum(prod_cnt) AS api_cnt        
FROM Temp1 a
GROUP BY district_lease, first_proddate
ORDER BY district_lease, first_proddate ;