22,181
社区成员




---测试数据---
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([年] int,[月] int,[科目代码] int,[客户代码] varchar(12),[期初余额] numeric(9,2),[本月借方发生] numeric(9,2),[本月贷方发生] numeric(9,2),[期末余额] numeric(9,2))
insert [tb]
select 2005,4,122,'01.053.00173',0.00,416820.00,416820.00,0.00 union all
select 2005,5,122,'01.053.00173',0.00,351000.00,0.00,351000.00 union all
select 2005,6,122,'01.053.00173',351000.00,8500.00,351000.00,8500.00 union all
select 2005,7,122,'01.053.00173',8500.00,780000.00,788500.00,0.00 union all
select 2005,8,122,'01.053.00173',0.00,1964810.00,194250.00,1770560.00 union all
select 2005,9,122,'01.053.00173',1770560.00,37000.00,792000.00,1015560.00 union all
select 2005,10,122,'01.053.00173',1015560.00,0.00,1015560.00,0.00 union all
select 2005,11,122,'01.053.00239',0.00,0.00,0.00,0.00 union all
select 2005,12,122,'01.053.00239',0.00,0.00,0.00,0.00 union all
select 2006,1,122,'01.053.00239',0.00,0.00,0.00,0.00 union all
select 2006,2,122,'01.053.00239',0.00,0.00,0.00,0.00 union all
select 2006,3,122,'01.053.00239',0.00,0.00,0.00,0.00 union all
select 2006,4,122,'01.053.00239',0.00,1807110.00,0.00,1807110.00 union all
select 2006,5,122,'01.053.00239',1807110.00,1346400.00,1330560.00,1822950.00 union all
select 2006,6,122,'01.053.00239',1822950.00,0.00,1346400.00,476550.00 union all
select 2006,7,122,'01.053.00239',476550.00,0.00,476550.00,0.00 union all
select 2006,8,122,'01.053.00239',0.00,0.00,0.00,0.00 union all
select 2006,9,122,'01.053.00239',0.00,0.00,0.00,0.00
---查询---
select * into #
from
(
select
px1=(select count(1) from tb where ltrim(年)+right('00'+ltrim(月),2)<ltrim(t.年)+right('00'+ltrim(t.月),2))/3+1,
px2=(select count(1) from tb where ltrim(年)+right('00'+ltrim(月),2)<ltrim(t.年)+right('00'+ltrim(t.月),2))%3+1,
px3=(select count(1) from tb where ltrim(年)+right('00'+ltrim(月),2)<ltrim(t.年)+right('00'+ltrim(t.月),2))+1,
*
from tb t
) tt
select *from #
select
年,月,科目代码,客户代码,
[3个月前期初余额]=isnull((select 期末余额 from # where px2=t.px2 and px1=t.px1-1),0),
[近3个月借方发生]=isnull((select sum(本月借方发生) from # where px1=t.px1 and px3<=t.px3),0),
[近3个月贷方发生]=isnull((select sum(本月贷方发生) from # where px1=t.px1 and px3<=t.px3),0),
期末余额
from # t
drop table #
---结果---
年 月 科目代码 客户代码 3个月前期初余额 近3个月借方发生 近3个月贷方发生 期末余额
----------- ----------- ----------- ------------ ----------- ---------------------------------------- ---------------------------------------- -----------
2005 4 122 01.053.00173 .00 416820.00 416820.00 .00
2005 5 122 01.053.00173 .00 767820.00 416820.00 351000.00
2005 6 122 01.053.00173 .00 776320.00 767820.00 8500.00
2005 7 122 01.053.00173 .00 780000.00 788500.00 .00
2005 8 122 01.053.00173 351000.00 2744810.00 982750.00 1770560.00
2005 9 122 01.053.00173 8500.00 2781810.00 1774750.00 1015560.00
2005 10 122 01.053.00173 .00 .00 1015560.00 .00
2005 11 122 01.053.00239 1770560.00 .00 1015560.00 .00
2005 12 122 01.053.00239 1015560.00 .00 1015560.00 .00
2006 1 122 01.053.00239 .00 .00 .00 .00
2006 2 122 01.053.00239 .00 .00 .00 .00
2006 3 122 01.053.00239 .00 .00 .00 .00
2006 4 122 01.053.00239 .00 1807110.00 .00 1807110.00
2006 5 122 01.053.00239 .00 3153510.00 1330560.00 1822950.00
2006 6 122 01.053.00239 .00 3153510.00 2676960.00 476550.00
2006 7 122 01.053.00239 1807110.00 .00 476550.00 .00
2006 8 122 01.053.00239 1822950.00 .00 476550.00 .00
2006 9 122 01.053.00239 476550.00 .00 476550.00 .00
(所影响的行数为 18 行)
select 年,科目代码,客户代码,SUM(期初余额),SUM(本月借方发生),sum(本月贷方发生),SUM(期末余额)
from 表 group by 年,datepart( quarter,(年+'-' +月 + '-'+ '00')),科目代码,客户代码
Declare @a Table (年 varchar(20), 月 varchar(20),科目代码 varchar(20), 客户代码 varchar(20),
期初余额 float, 本月借方发生 float,本月贷方发生 float,期末余额 float)
Insert @a Select
'2005','4','122','01.053.00173',0.00,416820.00,416820.00,0.00 Union All Select
'2005','5','122','01.053.00173',0.00,351000.00,0.00,351000.00 Union All Select
'2005','6','122','01.053.00173',351000.00,8500.00,351000.00,8500.00 Union All Select
'2005','7','122','01.053.00173',8500.00,780000.00,788500.00,0.00 Union All Select
'2005','8','122','01.053.00173',0.00,1964810.00,194250.00,1770560.00 Union All Select
'2005','9','122','01.053.00173',1770560.00,37000.00,792000.00,1015560.00 Union All Select
'2005','10','122','01.053.00173',1015560.00,0.00,1015560.00,0.00 Union All Select
'2005','11','122','01.053.00239',0.00,0.00,0.00,0.00 Union All Select
'2005','12','122','01.053.00239',0.00,0.00,0.00,0.00 Union All Select
'2006','1','122','01.053.00239',0.00,0.00,0.00,0.00 Union All Select
'2006','2','122','01.053.00239',0.00,0.00,0.00,0.00 Union All Select
'2006','3','122','01.053.00239',0.00,0.00,0.00,0.00 Union All Select
'2006','4','122','01.053.00239',0.00,1807110.00,0.00,1807110.00 Union All Select
'2006','5','122','01.053.00239',1807110.00,1346400.00,1330560.00,1822950.00 Union All Select
'2006','6','122','01.053.00239',1822950.00,0.00,1346400.00,476550.00 Union All Select
'2006','7','122','01.053.00239',476550.00,0.00,476550.00,0.00 Union All Select
'2006','8','122','01.053.00239',0.00,0.00,0.00,0.00 Union All Select
'2006','9','122','01.053.00239',0.00,0.00,0.00,0.00
select * from @a
年 月 科目代码 客户代码 期初余额 本月借方发生 本月贷方发生 期末余额
2005 4 122 01.053.00173 0 416820 416820 0
2005 5 122 01.053.00173 0 351000 0 351000
2005 6 122 01.053.00173 351000 8500 351000 8500
2005 7 122 01.053.00173 8500 780000 788500 0
2005 8 122 01.053.00173 0 1964810 194250 1770560
2005 9 122 01.053.00173 1770560 37000 792000 1015560
2005 10 122 01.053.00173 1015560 0 1015560 0
2005 11 122 01.053.00239 0 0 0 0
2005 12 122 01.053.00239 0 0 0 0
2006 1 122 01.053.00239 0 0 0 0
2006 2 122 01.053.00239 0 0 0 0
2006 3 122 01.053.00239 0 0 0 0
2006 4 122 01.053.00239 0 1807110 0 1807110
2006 5 122 01.053.00239 1807110 1346400 1330560 1822950
2006 6 122 01.053.00239 1822950 0 1346400 476550
2006 7 122 01.053.00239 476550 0 476550 0
2006 8 122 01.053.00239 0 0 0 0
2006 9 122 01.053.00239 0 0 0 0