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
相关推荐
一叶飘零_sweeeet1 分钟前
MongoDB 基础与应用
数据库·mongodb
猿小喵17 分钟前
DBA之路,始于足下
数据库·dba
tyler_download26 分钟前
golang 实现比特币内核:实现基于椭圆曲线的数字签名和验证
开发语言·数据库·golang
weixin_449310841 小时前
高效集成:聚水潭采购数据同步到MySQL
android·数据库·mysql
Dreams°1231 小时前
大数据 ETL + Flume 数据清洗 — 详细教程及实例(附常见问题及解决方案)
大数据·单元测试·可用性测试
sf_www2 小时前
Flink on YARN是如何确定TaskManager个数的
大数据·flink
Cachel wood2 小时前
Github配置ssh key原理及操作步骤
运维·开发语言·数据库·windows·postgresql·ssh·github
standxy2 小时前
如何将钉钉新收款单数据高效集成到MySQL
数据库·mysql·钉钉
Narutolxy3 小时前
MySQL 权限困境:从权限丢失到权限重生的完整解决方案20241108
数据库·mysql
Venchill3 小时前
安装和卸载Mysql(压缩版)
数据库·mysql