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
相关推荐
小白教程28 分钟前
MySQL数据库的安全性防护
数据库·mysql
心碎土豆块30 分钟前
MapReduce打包运行
大数据·mapreduce
Lion Long30 分钟前
CodeBuddy 中国版 Cursor 实战:Redis+MySQL双引擎驱动〈王者荣耀〉战区排行榜
数据库·redis·mysql·缓存·腾讯云·codebuddy首席试玩官·codebuddy
apcipot_rain3 小时前
【应用密码学】实验五 公钥密码2——ECC
前端·数据库·python
元6334 小时前
Spark 缓存(Caching)
大数据·spark
麻芝汤圆5 小时前
MapReduce 入门实战:WordCount 程序
大数据·前端·javascript·ajax·spark·mapreduce
IvanCodes6 小时前
五、Hadoop集群部署:从零搭建三节点Hadoop环境(保姆级教程)
大数据·hadoop·分布式
辛一一6 小时前
neo4j图数据库基本概念和向量使用
数据库·neo4j
富能量爆棚7 小时前
spark-local模式
大数据
lqlj22337 小时前
配置 Spark 以 YARN 模式
大数据·spark