mysql获取当月数据以及当月之前的累加数据

sql如下

SELECT a.foodVariety foodVariety,c.total weight,a.month month,SUM(b.total) total 
        FROM( SELECT   FOOD_VARIETY_ foodVariety,DATE_FORMAT(REGISTER_TIME_ , '%Y-%m') month,
        REGISTER_TIME_ registerTime,SUM(NET_WEIGHT_) total,TYPE_ type FROM   d_inout_stock  
        GROUP BY  FOOD_VARIETY_,month) AS a
JOIN ( SELECT   FOOD_VARIETY_ foodVariety ,DATE_FORMAT(REGISTER_TIME_, '%Y-%m') month
        ,SUM(NET_WEIGHT_) total 
        FROM  d_inout_stock where  GROUP BY foodVariety,month) AS b 
         ON a.foodVariety = b.foodVariety AND a.month >= b.month        
 JOIN ( SELECT FOOD_VARIETY_ foodVariety,DATE_FORMAT(REGISTER_TIME_, '%Y-%m') month
         ,SUM(NET_WEIGHT_) total 
 FROM d_inout_stock  where  GROUP BY foodVariety,month) AS c 
                    ON a.foodVariety = c.foodVariety AND a.month = c.month 

同时可以根据时间的不同格式进行日和周的累加
%Y-%m-%d 日统计
%Y-%m 月统计
%Y-%u 周统计

你可能感兴趣的:(SSIS以及数据库相关)