背景
统计某个指标,指标按照月进行累加,注意需要按省份和年份进行分组。
方法一、使用自关联
-- with 按月统计得到中间结果
WITH yms AS (SELECT regionid,SUM(getnum) AS getnum,SUM(dealnum) AS dealnum,to_char(qndate,"yyyy-MM") AS yearmonth
FROM t_queuenumber
GROUP BY regionid,to_char(qndate,"yyyy-MM")
ORDER BY regionid,yearmonth)-- 查用子查询解决。
SELECT s1.regionid,s1.yearmonth, getnum,dealnum,
(SELECT SUM(getnum) FROM yms s2 WHERE s2.regionid = s1.regionid AND s2.yearmonth <= s1.yearmonth AND SUBSTRING(s1.yearmonth,0,5) = SUBSTRING(s2.yearmonth,0,5) ) AS getaccumulatednum,
(SELECT SUM(dealnum) FROM yms s2 WHERE s2.regionid = s1.regionid AND s2.yearmonth <= s1.yearmonth AND SUBSTRING(s1.yearmonth,0,5) = SUBSTRING(s
最后
以上就是欢喜玫瑰最近收集整理的关于mysql 按月累计_PostgreSQL完成按月累加的操作的全部内容,更多相关mysql内容请搜索靠谱客的其他文章。
发表评论 取消回复