关于sql2005的sql的问题,急急急,在线等!

mangchao330688 2011-06-08 07:15:55
我有这样一张表
id name money money2 money3 month

1 张三 总10块 花5块 剩5块 1月

我要把1月剩的5块 拿到第2月的总数那里

2 张三 总5块 花5块 剩0块 2月


这个要用sql怎么写 或者存储过程怎么弄。。。。。急急急
还有sql存储过程循环计算怎么写 能给个例子吗。谢了。
...全文
154 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
快乐的小二兔 2011-06-09
  • 打赏
  • 举报
回复
这数据库设计的。。。霸气外露
syb1045 2011-06-09
  • 打赏
  • 举报
回复
很强大的需求..


create table testDemo
(
总 int ,
剩 int,
dates int ,
)

insert into testDemo values(100,80,1)
insert into testDemo values(80,60,2)
insert into testDemo values(60,40,3)
insert into testDemo values(40,20,4)

select * from testDemo
select case testDemo.dates when a.dates+1 then a.剩 end as money11,testDemo.剩,testDemo.dates from testDemo,
(select 剩,dates from testDemo) as a where testDemo.dates=a.dates+1

drop table testDemo


玩玩..
laowang134 2011-06-09
  • 打赏
  • 举报
回复
declare @tb table(id int,[name] varchar(20),[money] float, money2 float, money3 float, [month] int)
insert into @tb
select 1,'张三',10,5,5,1 union all
select 2,'张三',5,5,2,2 union all
select 3,'张三',10,5,1,3 union all
select 4,'张三',5,5,0,4 union all
select 5,'李四',10,5,5,1 union all
select 6,'李四',5,5,0,2 union all
select 7,'李四',10,5,5,3 union all
select 8,'李四',5,5,0,4

select id,[name],[money]=isnull([money]+(select money3 from @tb where [name]=a.[name] and [month]=a.[month]-1),[money])
,money2,money3,[month] from @tb a
/*
1 张三 10 5 5 1
2 张三 10 5 2 2
3 张三 12 5 1 3
4 张三 6 5 0 4
5 李四 10 5 5 1
6 李四 10 5 0 2
7 李四 10 5 5 3
8 李四 10 5 0 4
*/
laowang134 2011-06-09
  • 打赏
  • 举报
回复
declare @tb table(id int,[name] varchar(20),[money] float, money2 float, money3 float, [month] int)
insert into @tb
select 1,'张三',10,5,5,1 union all
select 2,'张三',5,5,2,2 union all
select 3,'张三',10,5,1,3 union all
select 4,'张三',5,5,0,4 union all
select 5,'李四',10,5,5,1 union all
select 6,'李四',5,5,0,2 union all
select 7,'李四',10,5,5,3 union all
select 8,'李四',5,5,0,4

select id,[name],[money]=isnull([money]+(select money3 from @tb where [name]=a.[name] and [month]=a.[month]-1),[money])
,money2,money3,[month] from @tb a
  • 打赏
  • 举报
回复
…… 晕。。。这表重做吧
叶子 2011-06-09
  • 打赏
  • 举报
回复
貌似这个问题你在SQL版问过,没有解决吗?
wubing1111 2011-06-09
  • 打赏
  • 举报
回复
可以用存储过程,不过不知道你这个是不是还有3月没花拿到4月之类的
如果只是你上面的两个月份的话:先判定1月有没有剩余如果有取值,你的表里如果有2月分的记录就更新,如果没有新增一笔。
yhtapmys 2011-06-08
  • 打赏
  • 举报
回复
用临时表
把每个用户的最大id(最后一条应该是最新的,如果是按时间月份插入的话)查出来放入临时表
再读取临时表 游标逐一取出每个用户的name money3
插入原表,产生每个用户最新的月份记录money2 money3字段为空--
统计花费的时候 再更新money2 money3(问题:根据什么更新?用户最大ID?name?)

即使写出来
那不得每个月必须得在恰当的时间(每月最后一天最后一秒?)跑一次存储过程?
建议如楼上诸位之言,将表设计优化下
这样的表适宜用逻辑呈现在报表中
子夜__ 2011-06-08
  • 打赏
  • 举报
回复
按月数 把money3

移下来当money ?

还是不得劲啊。

不知道LINQ能实现不。


yongpeng_china 2011-06-08
  • 打赏
  • 举报
回复
参考这个网址http://blog.csdn.net/yongpeng_china/archive/2011/06/04/6525609.aspx
里面有SQL高级编程,也有几个SQL编程的循环语句
qgqch2008 2011-06-08
  • 打赏
  • 举报
回复

id name total spend remain month

1 张三 10 5 5 2011-1

你的应该这么搞
qgqch2008 2011-06-08
  • 打赏
  • 举报
回复
表,没有这么设计的。
md5e 2011-06-08
  • 打赏
  • 举报
回复
我不是很明白楼主你为什么这样建表,你建的数据表都有问题,即不是流水帐,又不是储户帐,有点牛头不对马嘴的感觉,一般我们储存用户金额是一个表,流水帐是另一个表
inetfuture 2011-06-08
  • 打赏
  • 举报
回复
数据库设计不合理……

62,244

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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