首页 新闻 论坛 群组 Blog 文档 下载 读书 Tag 网摘 搜索 .NET Java 游戏 视频 人才 外包 培训 数据库 书店 程序员
中国软件网
欢迎您:游客 | 登录 注册 帮助
  • 复杂计算问题 [已结贴,结贴人:ffccdd612]
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-11 14:34:16 楼主
    有如下表,cc字段记录BB字段出现的次数,DD 代表金额

    aa bb cc dd
    1 b 1 12
    2 c 1 20
    3 b 2 20
    4 d 1 30
    5 a 1 40
    6 d 2 10
    7 d 3 12
    8 c 2 -11
    9 a 2 12
    10 b 3 -10
    11 d 4 -10

    现在要得到如下结果,如果DD字段出现负数,则删除此项记录,同时将DD字段金额累加到上一次出现记录上.
    如d出现4次,第四次为-10,则删除此项记录,同时将-10累加到d对应的第三次记录12上,结果为2
    下表我没有排序,主要为了容易区分


    aa bb cc dd
    5 a 1 40
    9 a 2 12
    1 b 1 12
    3 b 2 10
    2 c 1 9
    4 d 1 30
    6 d 2 10
    7 d 3 2

    ,谢谢高手帮忙!
    100  修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • ranzj
    • 等级:
    发表于:2008-05-11 14:57:331楼 得分:0
    我先把示例表建好,数据模拟好后,慢慢分析。有点头大。
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-11 15:17:302楼 得分:0
    有如下表,cc字段记录BB字段出现的次数,DD 代表金额

    模拟表如下:
    create table tb1(aa int,bb nvarchar(10),cc int,dd int)
    insert into tb1 select 1,'b',1,12
    insert into tb1 select 2,'c',1,20
    insert into tb1 select 3,'b',2,20
    insert into tb1 select 4,'d',1,30
    insert into tb1 select 5,'a',1,40
    insert into tb1 select 6,'d',2,10
    insert into tb1 select 7,'d',3,12
    insert into tb1 select 8,'c',2,-11
    insert into tb1 select 9,'a',2,12
    insert into tb1 select 10,'b',3,-10
    insert into tb1 select 11,'d',4,-10

    现在要得到如下结果,如果DD字段出现负数,则删除此项记录,同时将DD字段金额累加到上一次出现记录上.
    如d出现4次,第四次为-10,则删除此项记录,同时将-10累加到d对应的第三次记录12上,结果为2
    下表我没有排序,主要为了容易区分


    aa    bb    cc    dd
    5    a      1    40
    9    a      2    12
    1    b      1    12
    3    b      2    10
    2    c      1    9
    4    d      1    30
    6    d      2    10
    7    d      3      2


    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-11 16:07:143楼 得分:0
    自己顶一下
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-11 17:16:304楼 得分:40
    SQL code
    create table #tab(aa int,bb nvarchar(10),cc int,dd int) insert into #tab(aa,bb,cc,dd) select 1,'b',1,12 insert into #tab(aa,bb,cc,dd) select 2,'c',1,20 insert into #tab(aa,bb,cc,dd) select 3,'b',2,20 insert into #tab(aa,bb,cc,dd) select 4,'d',1,30 insert into #tab(aa,bb,cc,dd) select 5,'a',1,40 insert into #tab(aa,bb,cc,dd) select 6,'d',2,10 insert into #tab(aa,bb,cc,dd) select 7,'d',3,12 insert into #tab(aa,bb,cc,dd) select 8,'c',2,-11 insert into #tab(aa,bb,cc,dd) select 9,'a',2,12 insert into #tab(aa,bb,cc,dd) select 10,'b',3,-10 insert into #tab(aa,bb,cc,dd) select 11,'d',4,-10 select * from #tab order by bb,aa declare @aa int declare @bb varchar(10) declare @dd int declare curdel cursor static for select aa,bb,dd from #tab where dd < 0 open curdel fetch next from curdel into @aa,@bb,@dd while @@fetch_status = 0 begin update #tab set dd = dd + @dd where bb = @bb and aa = (select max(aa) from #tab where bb= @bb and aa < @aa) delete from #tab where bb = @bb and aa = @aa fetch next from curdel into @aa,@bb,@dd end close curdel deallocate curdel select * from #tab order by bb ,aa drop table #tab
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • dawugui
    • 等级:
    发表于:2008-05-11 17:42:015楼 得分:60
    SQL code
    create table tb1(aa int,bb nvarchar(10),cc int,dd int) insert into tb1 select 1,'b',1,12 insert into tb1 select 2,'c',1,20 insert into tb1 select 3,'b',2,20 insert into tb1 select 4,'d',1,30 insert into tb1 select 5,'a',1,40 insert into tb1 select 6,'d',2,10 insert into tb1 select 7,'d',3,12 insert into tb1 select 8,'c',2,-11 insert into tb1 select 9,'a',2,12 insert into tb1 select 10,'b',3,-10 insert into tb1 select 11,'d',4,-10 select * from ( select aa,bb,cc,dd = case when dd > 0 then isnull((select top 1 dd from tb1 where bb = t.bb and cc = t.cc + 1 and dd < 0 order by cc desc),0) + dd else dd end from tb1 t ) m where dd >= 0 order by bb, cc drop table tb1 /* aa bb cc dd ----------- ---------- ----------- ----------- 5 a 1 40 9 a 2 12 1 b 1 12 3 b 2 10 2 c 1 9 4 d 1 30 6 d 2 10 7 d 3 2 (所影响的行数为 8 行) */
    修改 删除 举报 引用 回复

    网站简介广告服务网站地图帮助联系方式诚聘英才English 问题报告
    世纪乐知(北京)网络技术有限公司 版权所有 京 ICP 证 020026 号
    Copyright © 2000-2007, CSDN.NET, All Rights Reserved