sql解决按月每天叠加,sql累加问题
问题描述:
拿到手的表,需要统计每天 sersive科目下当月的总量,即累计server科目 当月每天的num和
data month sersive num
2022-01-03 202201 前日余额 272761.98
2022-01-04 202201 前日余额 272812.98
2022-01-05 202201 前日余额 272996.43
目标表:
data month sersive num count
2022-01-04 202201 前日余额 175.60 1756005.88
2022-01-05 202201 前日余额 46.00 2216005.88
2022-01-06 202201 前日余额 0.00 2216005.88
2022-01-07 202201 前日余额 141.31 3629127.88
count 数据即每月server科目下每天的和
实现代码:
select
date
, month
,km
,sje
,sum(je) OVER(partition BY km ORDER BY datennn) AS count
from table
函数over介绍:
0ver不能单独使用,要和分析函数: rank(),dense_rank(),row_number()等一起使用。
其参数:over(partition by columnname1 order by columnname2)
**含义:**按columname1指定的字段进行分组排序,或者说按字段columnname1的值进行分组排序。
例如:employees表中,有两个部门的记录:department_id =10和20`
select
department_id
,rank()
over(partition by department_id order by salary)
from employees
就是指在部门10中进行薪水的排名,在部门20中进行薪水排名。如果是partition by org_id,则是在整个公司内进行排名。
over(partition by class order by sroce) 按照sroce排序进行累计,order by是个默认的开窗函数,按照class分区。
2、开窗的窗口范围:
over(order by sroce range between 5 preceding and 5 following):窗口范围为当前行数据幅度减5加5后的范围内的。
over(order by sroce rows between 5 preceding and 5 following):窗口范围为当前行前后各移动5行。
最后
以上就是生动薯片最近收集整理的关于sql解决按月每天叠加,sql累加问题sql解决按月每天叠加,sql累加问题函数over介绍:的全部内容,更多相关sql解决按月每天叠加内容请搜索靠谱客的其他文章。
发表评论 取消回复