27,579
社区成员
发帖
与我相关
我的任务
分享
create table test(用戶 varchar(20),年度 int,金額 int )
insert into test select '張三','1996','100'
insert into test select '張三','1997','150'
insert into test select '張三','1998','200'
insert into test select '李四','1996','120'
insert into test select '李四','1997','130'
insert into test select '李四','1998','140'
select
用戶,
年度,
[上年结余]=(select sum(金額) from test where 年度<a.年度 and 用戶=a.用戶),
金額 as '期初',[期末]=(select sum(金額) from test where 年度<=a.年度 and 用戶=a.用戶)
from test a
/*
用戶 年度 上年结余 期初 期末
-------------------- ----------- ----------- ----------- -----------
張三 1996 NULL 100 100
張三 1997 100 150 250
張三 1998 250 200 450
李四 1996 NULL 120 120
李四 1997 120 130 250
李四 1998 250 140 390
(6 row(s) affected)
*/
select a.用户,a.年度,
isnull(t.期末,0)=a.金额,
a.金额 as 期初, --?
isnull(t.期末,0) as 期末
from [一张表] a left join (
select 用户,年度,(select sum(金额) from [一张表] where 用户=b.用户 and 年度<=b.年度) as 期末
from [一张表] b
) as t
on a.用户=t.用户 and a.年度=t.年度
order by a.用户,a.年度
create table t(用户 varchar(10), 年度 varchar(10), 金额 int)
insert t
select '张三','1996', 100
union select '张三','1997', 150
union select '张三','1998', 200
union select '李四','1996', 120
union select '李四','1997', 130
union select '李四','1998', 140
select 用户,年度,
上年结余=isnull((select sum(金额) from t where 用户=a.用户 and 年度<a.年度),0),
期初=金额,
期末=isnull((select sum(金额) from t where 用户=a.用户 and 年度<=a.年度),0)
from t a
drop table t
/* 结果
用户 年度 上年结余 期初 期末
---------- ---------- ----------- ----------- -----------
李四 1996 0 120 120
李四 1997 120 130 250
李四 1998 250 140 390
张三 1996 0 100 100
张三 1997 100 150 250
张三 1998 250 200 450
(所影响的行数为 6 行)
*/
--建立测试环境
set nocount on
create table test(用户 varchar(20),年度 int,金额 int )
insert into test select '张三','1996','100'
insert into test select '张三','1997','150'
insert into test select '张三','1998','200'
insert into test select '李四','1996','120'
insert into test select '李四','1997','130'
insert into test select '李四','1998','140'
go
--测试
select a.用户,a.年度,isnull(b.金额,0) as 上年结余 ,isnull(a.金额,0) as 期初,isnull(a.金额,0)+isnull(b.金额,0) as 期末
from test a left join test b
on a.用户=b.用户 and a.年度=b.年度+1
--删除测试环境
drop table test
set nocount off
/*
用户 年度 上年结余 期初 期末
-------------------- ----------- ----------- ----------- -----------
张三 1996 0 100 100
张三 1997 100 150 250
张三 1998 150 200 350
李四 1996 0 120 120
李四 1997 120 130 250
李四 1998 130 140 270
*/
select 用户,年度,
isnull((select sum(金额) from [一张表] where 用户=a.用户 and 年度<a.年度),0) as 上年结余,
金额 as 期初, --?
isnull((select sum(金额) from [一张表] where 用户=a.用户 and 年度<=a.年度),0) as 期末
from [一张表] a
order by 用户,年度
select 用户,年度,
isnull((select sum(金额) from [一张表] where 用户=a.用户 and 年度<a.年度),0) as 上年结余,
金额 as 期初, --?
isnull((select sum(金额) from [一张表] where 用户=a.用户 and 年度<=a.年度),0) as 期末
from [一张表] a
order by 用户,年度
--建立测试环境
set nocount on
create table test(用户 varchar(20),年度 int,金额 int )
insert into test select '张三','1996','100'
insert into test select '张三','1997','150'
insert into test select '张三','1998','200'
insert into test select '李四','1996','120'
insert into test select '李四','1997','130'
insert into test select '李四','1998','140'
go
--测试
select a.用户,a.年度,isnull(b.金额,0) as 上年结余 ,isnull(a.金额,0) as 期初,isnull(a.金额+b.金额,0) as 期末
from test a left join test b
on a.用户=b.用户 and a.年度=b.年度+1
--删除测试环境
drop table test
set nocount off
/*用户 年度 上年结余 期初 期末
-------------------- ----------- ----------- ----------- -----------
张三 1996 0 100 0
张三 1997 100 150 250
张三 1998 150 200 350
李四 1996 0 120 0
李四 1997 120 130 250
李四 1998 130 140 270
*/
---try
select 用户,年度,
上年结余=isnull((select sum(金额) from 表 where 用户=a.用户 and 年度<a.年度),0),
期初=金额,
期末=isnull((select sum(金额) from 表 where 用户=a.用户 and 年度<=a.年度)-金额,0)
from 表