select * from Acc_Attest a
where not exists (
select 1 from Acc_Income
where User_ID=a.User_ID
and cYear=a.cYear
and cMonth-a.cMonth
)这应该是欠费明细表吧

解决方案 »

  1.   

    select User_ID,min(cast(cast(cYear as varchar(4))+cast(cMonth as varchar(2))+'01' as datetime)) as 最早时间,sum(tMoney) as 欠费总额 from Acc_Attest a
    where not exists (
    select 1 from Acc_Income
    where User_ID=a.User_ID
    and cYear=a.cYear
    and cMonth-a.cMonth
    )
    group by User_ID这是你要的吗?
      

  2.   

    更正一下手误水费收缴表
    Acc_IncomeInvoiceNo, 认证单号 //  这里应该是  发票号
    要显示出来的是如下明细用户名称  欠费年、月 欠费总额 ……目前没有解决的是,无法确定哪个月是最早欠费月。比如抄表
    年    月  金额  水量
    2006  1   100   200
    2006  2   120   240
    2006  3   110   220
    ……收费
    年    月  金额  水量
    2006  1   100   200
    2006  2   60    120
    2006  3   null  null   
    ……用户要求的思路如下:
    抄表的认证单按顺序累计一下,收费的同样累计一下,然后根据总额,确定从哪个月的抄表认证单开始欠费的。
      

  3.   

    这样处理吧
    先找出所有用户已经交了的费用总和
    写入用户缴费合计表
    select 用户编号=User_ID,缴费合计=sum(tMoney) into #1 from Acc_Income--得到这个表后再这样进行处理
    --按月份累加用户的应缴金额,找到第一个
    --应缴金额大于缴费合计的年月
      

  4.   

    第二步的累加可以参照如下:
    create table #1 (用户编号 int,缴费合计 money)insert into #1 
    select 1,200
    union all
    select 2,300
    Create table #2 (用户编号 int,水费 money,年 char(4) ,月 char(2))
    insert into #2
    select 1,50,'2005','10'
    union all 
    select 1,50,'2005','11'
    union all
    select 1,60,'2005','12'
    union all
    select 1,50,'2006','01'
    union all 
    select 1,50,'2006','02'
    union all 
    select 2,100,'2005','10'
    union all 
    select 2,50,'2005','11'
    union all 
    select 2,50,'2005','12'
    union all 
    select 2,50,'2006','01'
    union all 
    select 2,50,'2006','02'
    union all 
    select 2,50,'2006','03'
    union all 
    select 2,50,'2006','04'--select * from #1
    --select * from #2select a.用户编号,年,月
    from #2 a
    left join #1 c on a.用户编号=c.用户编号
    where not exists(select sum(水费) from #2 b
    where b.用户编号=a.用户编号 and (a.年+a.月)>=(b.年+b.月)
    group by b.用户编号 having sum(水费)<=c.缴费合计)drop table #1
    drop table #2
      

  5.   

    To:  ReViSion(和尚)  仁兄
    感谢你的帮助,给我很大的启发……