概述
背景
统计某个指标,指标按照月进行累加,注意需要按省份和年份进行分组。
方法一、使用自关联
-- 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 按月累计_PostgreSQL完成按月累加的操作所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复