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
相关推荐
云计算DevOps-韩老师11 分钟前
【网络云计算】2024第52周-每日【2024/12/26】小测-理论&实操-备份MySQL数据库并发送邮件-解析
linux·开发语言·网络·数据库·mysql·云计算·perl
2401_8504108321 分钟前
redis的持久化
数据库·redis·bootstrap
如雨随行202024 分钟前
Mysql事务
数据库·mysql·oracle
小刘鸭!1 小时前
Flink窗口window详解(分类、生命周期、窗口分配器、窗口函数、触发器)
大数据·flink
未来并未来2 小时前
深入解析MVCC中Undo Log版本底层存储读取逻辑
java·数据库·mysql
菜还不练就废了2 小时前
Java期末复习JDBC|网课笔记+校课总结
java·开发语言·数据库
BabyFish132 小时前
Oracle复合索引规则指南
数据库·oracle·索引·复合索引
出发行进2 小时前
Hive其九,排名函数,练习和自定义函数
大数据·数据仓库·hive·hadoop·数据分析
szxinmai主板定制专家2 小时前
【国产NI替代】基于全国产FPGA的16振动+2转速+8路IO口输入输出(24bits)256k采样率,高精度终端采集板卡
大数据·人工智能·fpga开发
i易2 小时前
C++ 运算符重载
java·开发语言·数据库