如何为查询中的每个组选择汇总


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 ; 

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

使用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 ; 

相关内容

  • 没有找到相关文章

最新更新