需求:按会计期间 ,统计要给每个客户付款的情况--1.会计期间表 KJQJ
Select TOP 1 NianYue From KJQJ Order By ID Desc --获取本期会计期间方法--2.数据表 DBjj
会计期间  客户代码  客户   入货金额   已付金额
200101 G001 集团A 2000.00  2000.00
200202 G001 集团A 45000.00  10000.00
200206 G001 集团A 65000.00 15000.00
201208 G001 集团A 95000.00 NULL200206 G002 集团B 65000.00 1500.00
201209 G002 集团B 95000.00 NULL
--统计数据格式列表如下
客户代码  客户 期初应付 本期应付 本期付款 期末应付
-----------  说明  (假如本期会计期间 查询得到是 200206)-----------------------------
  期初应付=入货金额-已付金额  (200206 前的所有数据)
  本期应付=入货金额  (200206入货金额总额)
  本期付款=已付金额  (200206已付金额总额)
  期末应付=入货金额-已付金额  (200206数据)

解决方案 »

  1.   

    select 客户代码,客户,
           期初应付=(select sum(入货金额)-sum(已付金额) from DBjj where 会计期间<'200206')
           本期应付=(select sum(入货金额) from DBjj where 会计期间>='200206'and 会计期间                    <'200207'),
           本期付款=(select sum(已付金额) from DBjj where 会计期间>='200206'and 会计期间                    <'200207'),
           期末应付=isnull(本期应付,0)-isnull(本期付款,0)
    from  DBjj 
           
          
           
      

  2.   

    select 客户代码,客户,
           期初应付=(select sum(入货金额)-sum(已付金额) from DBjj where 会计期间<'200206')
           本期应付=(select sum(入货金额) from DBjj where 会计期间>='200206'and 会计期间                    <'200207'),
           本期付款=(select sum(已付金额) from DBjj where 会计期间>='200206'and 会计期间                    <'200207'),
           期末应付=isnull(本期应付,0)-isnull(本期付款,0)
    from  DBjj 
           
          
           
      

  3.   


    select 客户代码,客户,
      期初应付=(select sum(入货金额)-sum(已付金额) from DBjj where 会计期间<'200206')
      本期应付=(select sum(入货金额) from DBjj where 会计期间>='200206'and 会计期间 <'200207'),
      本期付款=(select sum(已付金额) from DBjj where 会计期间>='200206'and 会计期间 <'200207'),
      期末应付=isnull(本期应付,0)-isnull(本期付款,0)
    from DBjj顺便吐槽下:
    CSDN不是最近升级了两次么,怎么还是频繁出问题
      

  4.   


    >200206的数据不用统计
    统计的结果 应该是
    客户代码 客户 期初应付 本期应付 本期付款 期末应付
    G001 集团A 35000.00 65000.00 1500.00 5000.00
    G002 集团B 0.00 65000.00 1500.00 5000.00
      

  5.   

    select 客户代码,客户,
       期初应付=(select sum(入货金额)-sum(已付金额) from DBjj b where 会计期间<'200206' and b.客户代码 = a.客户代码)
       本期应付=(select sum(入货金额) from DBjj c where 会计期间>='200206'and 会计期间 <'200207' and b.客户代码 = c.客户代码),
       本期付款=(select sum(已付金额) from DBjj d where 会计期间>='200206'and 会计期间 <'200207' and b.客户代码 = d.客户代码),
       期末应付=isnull((select sum(入货金额) from DBjj e where 会计期间>='200206'and 会计期间 <'200207' and b.客户代码 = a.客户代码),0)-isnull((select sum(已付金额) from DBjj d where 会计期间>='200206'and 会计期间 <'200207' and e.客户代码 = a.客户代码 ),0)
    from DBjj a  
      

  6.   


    这种方法 还是没分组啊Select ... From DBjj a  就会出现重复数据