一个SQL语句看起来简单但是怎么也写不出

iranjn 2006-04-29 06:46:37
下面的数据
dt count
2006-1-1 1
2006-1-2 3
2006-1-3 6
2006-1-4 2

我想出来的结果是这天的数据加上比前几天的数据的和
dt count
2006-1-1 1
2006-1-2 4
2006-1-3 10
2006-1-4 17
SQL Server 数据库中这个怎么写呀,能不能用尽量少的语句实现,而不写存储过程
...全文
245 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
iranjn 2006-04-30
  • 打赏
  • 举报
回复
对不起是写错了,但是别看不重要的呀,关键是语句呀,拜托了,大侠快来呀
pongjun64 2006-04-30
  • 打赏
  • 举报
回复
select a.dt,(select sum(b.count) from table1 where b.dt<=a.dt) as count from table1 a
todouwang 2006-04-30
  • 打赏
  • 举报
回复

select dt,count=(select sum(count) from table1 where dt<=a.dt) from table1 a
azsoft 2006-04-30
  • 打赏
  • 举报
回复
这样也行

create table # (dt varchar(10),[count] int)
insert into #
select '2006-01-01',1
union all
select '2006-01-02',3
union all
select '2006-01-03',6
union all
select '2006-01-04',3

go
select sum([count]) from #
go

select *,(select sum([count]) from # where datediff(second,dt,a.dt)>=0) from # a

go
drop table #
azsoft 2006-04-30
  • 打赏
  • 举报
回复

create table # (d int identity(1,1) ,dt varchar(10),[count] int)
insert into #
select '2006-01-01',1
union all
select '2006-01-02',3
union all
select '2006-01-03',6
union all
select '2006-01-04',3

go
select sum([count]) from #
go

select *,(select sum([count]) from # where datediff(second,dt,a.dt)>=0 and d<=a.d ) from # a

go
drop table #
163luheng 2006-04-30
  • 打赏
  • 举报
回复
var i,j,CountValue;
begin
adoq.sql.text:='select * from TABEL1';
adoq.exesql;
if adoq.RecordCount<=1 then
begin
exit;
end
else
begin
for i:=2 to RecordCount do
begin
j:=adoq.RecNo;
adoq.RecNo:=adoq.RecNo-1;
CountValue:=adoq.FieldByName('count').AsInteger;
adoq.RecNo:=j;
adoq.FieldByName('count').AsInteger:=adoq.FieldByName('count').AsInteger+CountValue;
adoq.next;
end;
end;
end;
slzzg 2006-04-29
  • 打赏
  • 举报
回复
学习。。
zhangjing1979666 2006-04-29
  • 打赏
  • 举报
回复
是不是写错了,2006-1-4 12???
Rubi 2006-04-29
  • 打赏
  • 举报
回复
用递归算法

CSDN论坛浏览器:http://CoolSlob.ys168.com/
Rubi 2006-04-29
  • 打赏
  • 举报
回复
有点麻烦啊!

先抢个位置

CSDN论坛浏览器:http://CoolSlob.ys168.com/
xingwang75 2006-04-29
  • 打赏
  • 举报
回复
SQL Server用得少,不过ORACLE倒是比较容易实现。

2,497

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 数据库相关
社区管理员
  • 数据库相关社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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