SQL语句问题

sinwing 2007-12-27 02:41:52
表B结构如下
Company nvarchar(2) –公司 主健
Agencyid nvarchar(10)—工号 主健
Productid nvarchar(3)—产品号 主健
Wrcvdate int –日期 主健
Fyp decimal(17,6)—首年保费
要求编写sql语句查询2006年8月1日~31日每一天的当月累计 fyp。

我的解决方法:
declare @wd int,
@num int
set @wd=20060801
set @num=20060832
while @wd<@num
begin
select sum(Fyp) from B where Wrcvdate<@wd
set @wd=@wd+1
end

请问有简单的方法吗?这个方法开销太大了。
...全文
234 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
sinwing 2007-12-27
  • 打赏
  • 举报
回复
那个a是什么?
utpcb 2007-12-27
  • 打赏
  • 举报
回复
我的就是统计8月每天的统计
utpcb 2007-12-27
  • 打赏
  • 举报
回复

select Wrcvdate ,(select sum(Fyp) from B t2 where t2.Wrcvdate<=t1.Wrcvdate and t2 .Wrcvdate >20060801 )
from B t1 where Wrcvdate >20060801 and Wrcvdate <20060831
sinwing 2007-12-27
  • 打赏
  • 举报
回复
不行,这样不是我要的结果,我要的结果是这样的:
例如统计到8月20日,就要把8月20日之前的所有Fyp都加起来。
-狙击手- 2007-12-27
  • 打赏
  • 举报
回复
declare @t table(d datetime,e int)

insert @t select '2007-12-1',1
insert @t select '2007-12-2',1
insert @t select '2007-12-3',1
insert @t select '2007-12-4',1
insert @t select '2007-12-5',1
insert @t select '2007-12-6',1
insert @t select '2007-12-7',1

select d,(select sum(e) from @t where d<= a.d)
from @t a

/*

d
------------------------------------------------------ -----------
2007-12-01 00:00:00.000 1
2007-12-02 00:00:00.000 2
2007-12-03 00:00:00.000 3
2007-12-04 00:00:00.000 4
2007-12-05 00:00:00.000 5
2007-12-06 00:00:00.000 6
2007-12-07 00:00:00.000 7

(所影响的行数为 7 行)

*/
liangCK 2007-12-27
  • 打赏
  • 举报
回复
select sum(fyp)
from b
where wrcvdate between '2006-08-01' and '2006-08-31'
group by wrcvdate

34,836

社区成员

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

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