SQL server 里按年按月进行累计值统计

SQL server 里按年按月进行累计值统计

即 4月 算从1~4月的累计值

参考如下:

select dt,

account_set_ord,

account_set_title,

account_ord,

account_title,

year,

ym,

cumulative_money_J,

cumulative_money_D,

row_num

from (

SELECT

dt,

account_set_ord,

account_set_title,

account_ord,

account_title,

year,

ym,

cumulative_money_J,

cumulative_money_D,

ROW_NUMBER() OVER (PARTITION BY account_set_ord, account_set_title, account_ord, account_title, year, ym,cumulative_money_J, cumulative_money_D ORDER BY ym) as row_num

from (

SELECT

@DateThreshold as dt,

account_set_ord,

account_set_title,

account_ord,

account_title,

year,

ym,

SUM(income_money) OVER (PARTITION BY account_set_ord, account_set_title, account_ord, account_title, year ORDER BY ym) AS cumulative_money_J,

SUM(expense_money) OVER (PARTITION BY account_set_ord, account_set_title, account_ord, account_title, year ORDER BY ym) AS cumulative_money_D

FROM dws_finance_fund_balance_income_expense_month

WHERE dt = @DateThreshold

) t1

) t4

where t4.row_num = 1

采用 SUM(expense_money) OVER (PARTITION BY account_set_ord, account_set_title, account_ord, account_title, year ORDER BY ym) 来计算累计值

通过 ROW_NUMBER() OVER (PARTITION BY account_set_ord, account_set_title, account_ord, account_title, year, ym,cumulative_money_J, cumulative_money_D ORDER BY ym) as row_num 来进行过滤

复制代码
INSERT INTO dbo.dws_finance_fund_balance_month ([dt],[year],[quarter],[ym],[account_set_ord],[account_set_title],[account_ord],[account_title],[income_money],[cumulative_income_money],[expense_money],[cumulative_expense_money])
		SELECT t1.[dt]
			  ,t1.[year]
			  ,t1.[quarter]
			  ,t1.[ym]
			  ,t1.[account_set_ord]
			  ,t1.[account_set_title]
			  ,t1.[account_ord]
			  ,t1.[account_title]
			  ,t1.[income_money]
			  ,t2.cumulative_money_J
			  ,t1.[expense_money]
			  ,t2.cumulative_money_D
		from ( select 
				   [dt]
				  ,[year]
				  ,[quarter]
				  ,[ym]
				  ,[account_set_ord]
				  ,[account_set_title]
				  ,[account_ord]
				  ,[account_title]
				  ,[income_money]
				  ,[expense_money]
			  from dbo.dws_finance_fund_balance_income_expense_month where dt = @DateThreshold
		)t1
		left join (
				   select dt,
						account_set_ord, 
						account_set_title, 
						account_ord,
						account_title,
						year,
						ym,
						cumulative_money_J,
						cumulative_money_D,
						row_num
				  from ( 
						SELECT 
							dt,
							account_set_ord, 
							account_set_title, 
							account_ord,
							account_title,
							year,
							ym,
							cumulative_money_J,
							cumulative_money_D,
							ROW_NUMBER() OVER (PARTITION BY account_set_ord, account_set_title, account_ord, account_title, year, ym,cumulative_money_J, cumulative_money_D ORDER BY ym) as row_num
					  from ( 
							SELECT 
									@DateThreshold as dt,
									account_set_ord, 
									account_set_title, 
									account_ord,
									account_title,
									year,
									ym,
									SUM(income_money) OVER (PARTITION BY account_set_ord, account_set_title, account_ord, account_title, year ORDER BY ym) AS cumulative_money_J,
									SUM(expense_money) OVER (PARTITION BY account_set_ord, account_set_title, account_ord, account_title, year ORDER BY ym) AS cumulative_money_D			
							FROM dws_finance_fund_balance_income_expense_month
							WHERE dt = @DateThreshold
					  ) t1
				  ) t4
				  where t4.row_num = 1
		) t2
		on t1.account_set_ord = t2.account_set_ord and t1.account_ord = t2.account_ord and t1.year = t2.year and t1.ym = t2.ym
相关推荐
永洪科技3 小时前
永洪科技荣获商业智能品牌影响力奖,全力打造”AI+决策”引擎
大数据·人工智能·科技·数据分析·数据可视化·bi
计算机毕设定制辅导-无忧学长3 小时前
西门子 PLC 与 Modbus 集成:S7-1500 RTU/TCP 配置指南(一)
服务器·数据库·tcp/ip
weixin_307779133 小时前
Hive集群之间迁移的Linux Shell脚本
大数据·linux·hive·bash·迁移学习
程序员柳4 小时前
基于微信小程序的校园二手交易平台、微信小程序校园二手商城源代码+数据库+使用说明,layui+微信小程序+Spring Boot
数据库·微信小程序·layui
梦在深巷、4 小时前
MySQL/MariaDB数据库主从复制之基于二进制日志的方式
linux·数据库·mysql·mariadb
IT乌鸦坐飞机4 小时前
ansible部署数据库服务随机启动并创建用户和设置用户有完全权限
数据库·ansible·centos7
IT_10244 小时前
Spring Boot项目开发实战销售管理系统——数据库设计!
java·开发语言·数据库·spring boot·后端·oracle
祁思妙想5 小时前
八股学习(三)---MySQL
数据库·学习·mysql
惊骇世俗王某人6 小时前
1.MySQL之如何定位慢查询
数据库·mysql
上海锝秉工控6 小时前
防爆拉线位移传感器:工业安全的“隐形守护者”
大数据·人工智能·安全