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
相关推荐
V+zmm10134几秒前
机电公司管理信息系统小程序+论文源码调试讲解
java·数据库·微信小程序·小程序·毕业设计
何怀逸17 分钟前
MySQL的buffer pool的缓存中,修改语句在执行时,是先更新buffer pool,还是先更新数据库
数据库·mysql·缓存
atbigapp.com1 小时前
AI数据分析:一键生成可视化分析思路
大数据·人工智能·数据分析
deadknight91 小时前
表、索引统计信息锁定和解锁
数据库·oracle
yinghuabanwo1 小时前
【1688】崖山集群YAC安装备忘
数据库·崖山yashandb
不剪发的Tony老师1 小时前
PostgreSQL 18新特性之虚拟生成列
数据库·postgresql
考虑考虑1 小时前
UNION和UNION ALL的用法与区别
数据库·后端·mysql
叶域2 小时前
正则表达式(复习)
大数据·python·正则表达式
CL_IN2 小时前
如何将聚水潭·奇门平台数据高效集成到MySQL
android·数据库·mysql
Data-Miner2 小时前
电商数据分析 电商平台销售数据分析 电商平台数据库设计 揭秘电商怎么做数据分析
大数据