Select 的疑问

onlie 2008-01-07 01:28:32
假设数据库中存这样一张表,表结构如下:

用户 年度 金额

张三 1996 100
张三 1997 150
张三 1998 200
李四 1996 120
李四 1997 130
李四 1998 140

我要生成这样一个结果:

用户 年度 上年结余 期初 期末
张三 1996 0 100 100
张三 1997 100 150 250
张三 1998 250 200 450
李四 1996 0 120 120
........

总的来说"'上年结余上年的期末',而期末等于'上年结余+期初'"
求一条Select语名完成上述功能,谢谢
...全文
151 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
onlie 2008-01-07
  • 打赏
  • 举报
回复
看见大家这样,真是热盈框,谢谢大家.
changjiangzhibin 2008-01-07
  • 打赏
  • 举报
回复
up
fa_ge 2008-01-07
  • 打赏
  • 举报
回复

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)
*/
昵称被占用了 2008-01-07
  • 打赏
  • 举报
回复
剪刀的答案好像错了
昵称被占用了 2008-01-07
  • 打赏
  • 举报
回复
CSDN有点问题,如下效率可能高些


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.年度

leo_lesley 2008-01-07
  • 打赏
  • 举报
回复
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 行)
*/
jinjazz 2008-01-07
  • 打赏
  • 举报
回复
--建立测试环境
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

*/
昵称被占用了 2008-01-07
  • 打赏
  • 举报
回复

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 用户,年度
昵称被占用了 2008-01-07
  • 打赏
  • 举报
回复

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 用户,年度
jinjazz 2008-01-07
  • 打赏
  • 举报
回复
--建立测试环境
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

*/
leo_lesley 2008-01-07
  • 打赏
  • 举报
回复

---try
select 用户,年度,
上年结余=isnull((select sum(金额) from 表 where 用户=a.用户 and 年度<a.年度),0),
期初=金额,
期末=isnull((select sum(金额) from 表 where 用户=a.用户 and 年度<=a.年度)-金额,0)
from 表

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧