各位大侠高手来帮忙,脑乱ING,请指教 在线等 解决上分!

hahaha1983 2009-09-08 08:51:43
就是我的tb 里面有a,b,workdate 字段a为销售额,b为进货金额workdate为日期
我现在想要求出每天的时点利润
比如说从第一天开始 时点利润为a1-b1 时间为workdate1

第二天为(a1-b1)+(a2-b2) 时间为workdate2
第三天为(a1-b1)+(a2-b2)+(a3-b3) 时间为workdate3
以此类推
怎么样在存储过程中实现呢?
...全文
103 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
华夏小卒 2009-09-09
  • 打赏
  • 举报
回复
select 
*,
利润=(select sum(a-b) from tb where workdate<=t.workdate)
from
tb


这个肯定没错
zhangjiang264 2009-09-08
  • 打赏
  • 举报
回复
有复习了一下子查询的执行过程。
学习了
hahaha1983 2009-09-08
  • 打赏
  • 举报
回复
麻烦 明天高手9点在线等下吧 测试成功我马上结贴
如果还有问题的话 我再开贴 给分
能告诉我联系方式最好啦 直接开贴给分
hahaha1983 2009-09-08
  • 打赏
  • 举报
回复
告诉我撒
soft_wsx 2009-09-08
  • 打赏
  • 举报
回复
可以的
hahaha1983 2009-09-08
  • 打赏
  • 举报
回复
明天我测试下 高手能不能留个联系方式呢?比如QQ之类的
明天弄成功马上上分!
soft_wsx 2009-09-08
  • 打赏
  • 举报
回复
select *, 
(select sum(a-b) from tb where workdate<=t.workdate)
as 利润 from tb t
soft_wsx 2009-09-08
  • 打赏
  • 举报
回复
楼主是想要即时的结存数据呀!
百年树人 2009-09-08
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 hahaha1983 的回复:]
我这边没有SQL 能不能测试下看下结果呢?
a1=100 b1=50 日期随便从20090101开始好了
a2=200 b2=150
a3=300 b3=214
a4=0  b4=0
[/Quote]
---测试数据---
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([a] int,[b] int,[workdate] datetime)
insert [tb]
select 100,50,'2009-01-01' union all
select 200,150,'2009-01-02' union all
select 300,214,'2009-01-03' union all
select 0,0,'2009-01-03'

---查询---
select
*,
利润=(select sum(a-b) from tb where workdate<=t.workdate)
from
tb t

---结果---
a b workdate 利润
----------- ----------- ------------------------------------------------------ -----------
100 50 2009-01-01 00:00:00.000 50
200 150 2009-01-02 00:00:00.000 100
300 214 2009-01-03 00:00:00.000 186
0 0 2009-01-03 00:00:00.000 186

(所影响的行数为 4 行)
soft_wsx 2009-09-08
  • 打赏
  • 举报
回复
给出表结构+数据+想要的结果!在线等
xiequan2 2009-09-08
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 hahaha1983 的回复:]
我这边没有SQL 能不能测试下看下结果呢?
a1=100 b1=50 日期随便从20090101开始好了
a2=200 b2=150
a3=300 b3=214
a4=0  b4=0
[/Quote]
 
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([a] int,[b] int,[workdate] datetime)
insert [tb]
select 100,50,'20090101' union all
select 200,150,'20090102' union all
select 300,214,'20090103' union all
select 0,0,'20090104'

--select * from [tb]

select *, (select sum(a-b) from tb where workdate<=t.workdate) as 利润 from tb t

/*
a b workdate 利润
----------- ----------- ----------------------- -----------
100 50 2009-01-01 00:00:00.000 50
200 150 2009-01-02 00:00:00.000 100
300 214 2009-01-03 00:00:00.000 186
0 0 2009-01-04 00:00:00.000 186

(4 行受影响)


*/
hahaha1983 2009-09-08
  • 打赏
  • 举报
回复
我这边没有SQL 能不能测试下看下结果呢?
a1=100 b1=50 日期随便从20090101开始好了
a2=200 b2=150
a3=300 b3=214
a4=0 b4=0
xiequan2 2009-09-08
  • 打赏
  • 举报
回复
select *, (select sum(a-b) from tb where workdate<=t.workdate) as 利润 from  tb t
tb的别名
百年树人 2009-09-08
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 hahaha1983 的回复:]
这只是算出每天的利润呀 你的t.workdate 是指什么?
[/Quote]

是按日期累加的利润,用的是子查询,你可以贴点数据上来测试一下
hahaha1983 2009-09-08
  • 打赏
  • 举报
回复
这只是算出每天的利润呀 你的t.workdate 是指什么?
--小F-- 2009-09-08
  • 打赏
  • 举报
回复
select 
*,
(select sum(a-b) from tb where workdate<=t.workdate) as 利润
from
tb
百年树人 2009-09-08
  • 打赏
  • 举报
回复
select 
*,
利润=(select sum(a-b) from tb where workdate<=t.workdate)
from
tb

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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