另外100分在
http://expert.csdn.net/Expert/topic/1628/1628622.xml?temp=.5162317这里有三张表,三张之间以bianhao(编号)相连:table1---主表:bianhao(编号)
name(姓名)
shangqiyue(上期余额)
tingjitime(停机时间)table2---应收费用
bianhao(编号)
yuefen(月份)
feiyong1(费用1)
feiyong2(费用2)
otherfeiyong(其他费用)其中各月的:feiyong1(费用1)+feiyong2(费用2)+otherfeiyong(其他费用)=本期已收金额
table3----已收费用 (本期发生的所有的费用全部放在一个表中)bianhao(编号)
shoufeitime(收费时间)
shoufeijine(收费金额)其中:所有的收费金额之和为本期应收金额:
最后这三张表连接起来又形成了一个公式:shangqiyue(上期余额)+本期已收金额-本期应收金额=当前余额(若当前余额<0就表示欠费)
这时候,我想在一个窗口的DBgrid控件里把当前没有停机的所有的欠费的bianhao都出来。。应该怎么做?我举个实例吧:例如
table1中有记录如下:(13611111111 上期有余额200)bianhao       name   shangqiyue     tingjitime13611111111   小娇      200
13622222222   老王      300table2中记录如下:  (13611111111 本期各月应收费用之和为1500+1800+900=4200) bianhao        yuefen  feiyong1    feiyong2   otherfei   13611111111      1        500         500       500    (总和 ---1500)
13611111111      2        600         600       600    (总和---1800)
13611111111      3        300         300       300    (总和--900)
13622222222      1        400         400       400    (总和--1200)
table3记录如下:   (13611111111 本期的交费总和为  1500+1800+600= 3900)  bianhao            shoufeitime              shoufeijine13611111111          2003-2-15                  1500
13611111111          2003-3-15                  1800
13611111111          2003-4-15                  600
13622222222          2003-4-15                  700
      这时候我需要查出有哪些人欠费:13611111111的当前余额=3900+200-4200=-100 表示这个号欠费。
13622222222的当前余额=700+300-1200=-200  表示这个号也欠费。  这个时候我需要在DBGrid显示出来的信息应该是:bianhao  shangqiyue(上期余额)  yingshou(本期应收)  yishou(本期已收) jiyu(当前余额)13611111111        200          4200                  3900                -100
13622222222        300          1200                  700                 -200 我应该怎么做?

解决方案 »

  1.   

    累加交费总和:select bianhao, sum(shoufeijine) shoufeijine from table3 
    group by bianhao
      

  2.   

    select bianhao, sum(feiyong1) feiyong1,sum(feiyong2) feiyong2 ,sum(otherfei) otherfei  from table2 
    group by bianhao
      

  3.   

    谢谢careerist() 。不过。。不会这么简单吧?因为有三个表相连啊
      

  4.   

    上面两条语句是把table2和table3变成一个编号一条记录.
      

  5.   

    你的SQl语句中怎么没出现:table1,table3这样的字眼??难道这两个表你没用上?
      

  6.   

    1、累加交费总和:select bianhao, sum(shoufeijine) shoufeijine 
    from table3 
    group by bianhao
    2、select bianhao, sum(feiyong1) feiyong1,sum(feiyong2) feiyong2 ,sum(otherfei) otherfei  
    from table2 
    group by bianhao
      

  7.   

    select table1.bianhao from 
      (select bianhao,sum(feiyong1) as f1,sum(feiyong2) as f2, sum(otherfei) as f3    from table2 group by bianhao ) as a,
      (select bianhao, sum(shoufeijine) as sf from table3 group by bianhao ) as b
    where
      table1.shangqiyue+b.sf-a.f1-a.f2-a.f3<0 and
      table1.bianhao=a.bianhao and b.bianhao=a.bianhao
      

  8.   

    按你这么说。。分两步来做是吗??那么。。整体应该怎么搞才能达到我说的要求呢?不好意思。。我刚刚接触delphi,很菜,谢谢!!!!
      

  9.   

    你可以对每个表进行按BH,SUM(金额) 然后再用QUERY1查询,把余额作为计算字段
      

  10.   

    SQL语句如下:
    select a.bianhao, shangqiyue,sum(feiyong1+feiyong2+otherfei) yingshou,
    sum(shoufeijine) yishou ,(yishou-yingshou) jiyu  
    from table1 a,table2 b,table3 c 
    group by a.bianhao,shangqiyue
      

  11.   

    dbgrid.datasource.dataset.sql.close;
    dbgrid.datasource.dataset.sql.text:=
    select bianhao, sum(feiyong1)+sum(feiyong2)+sum(otherfei) as yingshoufei into #temp1 group by bianhao select bianhao, sum(shoufeijine) as jiaofei into #temp2 group by bianhao select a.bianhao,b.yingshoufei ,c.jiaofei ,a.shangqiyue+c.jiaofei-b.yingshoufei as danqianyue where (b.bianhao=a.bianhao) and (c.bianhao=a.bianhao)
    drop #temp1
    drop #temp2
    dbgrid.datasource.dataset.sql.Open;
      

  12.   

    select table2.bianhao sum(feiyong1+feiyong2+otherfee) allfee,sum(table2.shangqiyue) as yue,sum(table3.shoufeejine) as yijiaofee,
    (case when sum(table2.shangqiyue) is null then 0 else sum(table2.shangqiyue) end)+(case when sum(table3.shoufeejine) is null then 0 else sum(table3.shoufeejine) end)-(case when sum(feiyong1+feiyong2+otherfee) is null then 0 else sum(feiyong1+feiyong2+otherfee) end) jieyu from table2 left outer join table1 on table2.bianhao=bable1.banhao left outer join table3 on table1.bianhao=table3.bianhao group by table2.bianhao where jieyu<0
      

  13.   

    select a.bianhao, shangqiyue,sum(feiyong1+feiyong2+otherfei) yingshou,
    sum(shoufeijine) yishou ,(yishou-yingshou) jiyu  
    from table1 a,table2 b,table3 c 
    where jiyu<0
    group by a.bianhao,shangqiyue
      

  14.   

    select bianhao, sum(feiyong1)+sum(feiyong2)+sum(otherfei) as yingshoufei into #temp1 group by bianhao select bianhao, sum(shoufeijine) as jiaofei into #temp2 group by bianhao select a.bianhao,b.yingshoufei ,c.jiaofei ,a.shangqiyue+c.jiaofei-b.yingshoufei as danqianyue where (b.bianhao=a.bianhao) and (c.bianhao=a.bianhao)
    有误,改成:
    select bianhao, sum(feiyong1)+sum(feiyong2)+sum(otherfei) as yingshoufei into #temp1 from table2 group by bianhao select bianhao, sum(shoufeijine) as jiaofei into #temp2 from table3 group by bianhao select a.bianhao,b.yingshoufei ,c.jiaofei ,a.shangqiyue+c.jiaofei-b.yingshoufei as danqianyue from table1 a, #temp1 b #temp2 c where (b.bianhao=a.bianhao) and (c.bianhao=a.bianhao)
      

  15.   

    Taken(铁拳) 写的好像不全。。我看他写的sql好像只显示了kahao其他费用统计没有显示出来。天。。这下子要多长了。
      

  16.   

    TO  cjfzy(他山之石,可以攻玉。) temp1,temp2是怎么回事?什么类型的东东?是临时表吗?我用的是delphi+access。
      

  17.   

    select bianhao, shangqiyue, yingshou =
    (select sum(feiyong1+feiyong2+otherfeiyong) from table2 where bianhao=table1.bianhao),
    yishou =
    (select sum(shoufeijine) from table3 where bianhao=table1.bianhao),
    jiyu = 
    shangqiyue + (select sum(shoufeijine) from table3 where bianhao=table1.bianhao) - 
    (select sum(feiyong1+feiyong2+otherfeiyong) from table2 where bianhao=table1.bianhao)
    from table1 where tingjitime IS NULL and
    shangqiyue + (select sum(shoufeijine) from table3 where bianhao=table1.bianhao) - 
    (select sum(feiyong1+feiyong2+otherfeiyong) from table2 where bianhao=table1.bianhao)<0
      

  18.   

    按认真写了,并且认为是对的,楼主如果测试通过了要给俺结账哦:)俺的名字叫taken(铁拳)
      

  19.   

    这样写:select a. bianhao, 
    a.shangqiyue, 
    (select sum(b.feiyong1+b.feiyong2+b.otherfei) from b where b.bianhao=a.bianhao) as yingshou, 
    (select sum(c.shoufeijine) from c where b.bianhao=a.bianhao) as yishou, (yishou-yingshou) as jiyu 
    from table1 a, table2 b, table3 c where a.bianhao=b.bianhao and a.bianhao=c.bianhao order by a.bianhao
      

  20.   

    楼主要注意考虑一下table3和table1中如果没有和table2中对应的编号的问题。
      

  21.   

    楼主另外一个贴子里面应该已经有几个正确的了:)Study
      

  22.   

    实际中字段还要多得多。。妈的。光是这条sql我就写了很长还得用两个string字段增加到sql中。。ADOQuery_SaleKaSum.SQL.Add(str1+str2);现在还没搞对。。慢慢搞吧。。to:weitao999(涛涛) 。。table1是主表。。你说的情况我还真没考虑到。。怎么办怎么办怎么办。。等常规的解决了再来看特例吧。。脑子痛。。
      

  23.   

    这样写:
    select a. bianhao, 
    a.shangqiyue, 
    (select sum(b.feiyong1+b.feiyong2+b.otherfei) from table2 where b.bianhao=a.bianhao) as yingshou, 
    (select sum(c.shoufeijine) from table3 where b.bianhao=a.bianhao) as yishou, (yishou-yingshou) as jiyu 
    from table1 a, table2 b, table3 c where a.bianhao=b.bianhao and a.bianhao=c.bianhao order by a.bianhao
      

  24.   

    Wally_wu(韦利) 我是按你的方法来写的。不过发生了一件事令我百思不得其解。就是假如13611111111在table2中的记录中有4条,在table3的记录中有 3条。。那么按你的sql查询出来的13611111111的记录就是4*3=12条。。其中的数据也各不相同。。形成了排列组合的形式。。我实在搞不清为什么了:(
      

  25.   

    chenquan(嘉威王子),你的方法对了。。可以统计出来。。但有一个问题,就是像 weitao999(涛涛) 提醒的那样。假如table1中有13611111111的记录,而table2或者table3表中没有关于13611111111的话,那么关于13611111111的记录就没显示出来。。应该怎么办呢?后悔没学好数据库。。来不及看书了。:(。。
      

  26.   

    例如在table1中有13611111111的记录如下:bianhao       name   shangqiyue     tingjitime13611111111   小娇      -200但在table2和table3中都不存在13611111111的记录。。或者其中一个表中不存在。那么这条记录就没有显示出来。。
      

  27.   

    得看你的几个表是怎么生成的
    你的table2为应付,可以不管(该付款认为0,则不欠费)
    字段必须一一对应
    select * from table1 a,table2 b
    where a.bianhao=b.bianhao and 
    a.bianhao not in (select bianhao from table3)
    union 
      select .......(我原来写的东西)
      

  28.   

    你说的是特例只是13611111111不在table3中的一种情况啊:(
      

  29.   

    也有可能出现这种情况啊。。比方说:13611111111的上期余额为:-200它在table2中一条记录也没有只在table3K中有一条还款的记录。这时候那段代码就不起作用了啊。。
      

  30.   

    Create Table #Temp1 (bianhao int,shangqiyue decimal(19,2),feiyong decimal(19,2),
        shoufeijine decimal(19.2),Yue decimal(19,2) )
    insert into #Temp1 select bianhao,shangqiyue,0,0,0 from table1
    update #Temp1 set feiyong=sum(feiyong1+feiyong2+otherfeiyong) from #Temp1 a where #Temp1.bianhao=a.bianhao
           group by a.bianhao
    update #Temp1 set shoufeijine=sum(shoufeijine) from #Temp1 a where #Temp1.bianhao=a.bianhao
           group by a.bianhao
    update #Temp1 set yue=shangqiyue+shoufeijine-feiyong 
    select * from #Temp1 a,table b where a.bianhao=b.bianhao and a.yue<0 and b.tingtime<>'' and b.tingtime>getdate()
    drop table #Temp1
    再试试
      

  31.   

    不能用存储。。我用的是delphi+access!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
      

  32.   

    为了感谢各位朋友的帮忙:我又加了100分。。请去再回贴。。问题彻底解决后我好加分。:http://expert.csdn.net/Expert/topic/1643/1643422.xml?temp=.9598963
      

  33.   

    我的写错了,应该改一下的。用了case语句,防止table2和table3中为空的情况select table1.bianhao,table1.shangqiyue,sum(feiyong1+feiyong2+otherfee) allfee,sum(table3.shoufeejine) as yijiaofee,
    (case when sum(table2.shangqiyue) is null then 0 else sum(table2.shangqiyue) end)+(case when sum(table3.shoufeejine) is null then 0 else sum(table3.shoufeejine) end)-(case when sum(feiyong1+feiyong2+otherfee) is null then 0 else sum(feiyong1+feiyong2+otherfee) end) jieyu from table1 left outer join table2 on table1.bianhao=table2.banhao left outer join table3 on table1.bianhao=table3.bianhao where 
    (case when sum(table2.shangqiyue) is null then 0 else sum(table2.shangqiyue) end)+(case when sum(table3.shoufeejine) is null then 0 else sum(table3.shoufeejine) end)-(case when sum(feiyong1+feiyong2+otherfee) is null then 0 else sum(feiyong1+feiyong2+otherfee) end)<0 group by table1.bianhao,shangqiyue