同一sessionid同一alcode下的amt要累加后的amt<0

解决方案 »

  1.   

    select * from 表 
    where rundate between '2004-07-01' and '2004-08-01'
    and alcode='00001'
    and amt>0
    union
    select * from 表 a
    where alcode='00001'
    and(select sum(amt) from 表 where sessionid=a.sessionid)<0
      

  2.   

    select amt=sum(amt),sessionid , alcode from tb where amt>=0 convert(varchar(10),rendate,120) between  '2004-07-01' and '2004-08-01' group by sessionid , alcode 
    union all
    select amt=sum(amt),sessionid , alcode from tb where amt<0 convert(varchar(10),rendate,120) between  '2004-07-01' and '2004-08-01' group by sessionid , alcode 
      

  3.   

    上面我少個條件了select amt=sum(amt),sessionid , alcode from tb where amt>=0 and alcode='00001' and convert(varchar(10),rendate,120) between  '2004-07-01' and '2004-08-01' group by sessionid , alcode 
    union all
    select amt=sum(amt),sessionid , alcode from tb where amt<0 and alcode='00001' and convert(varchar(10),rendate,120) between  '2004-07-01' and '2004-08-01' group by sessionid , alcode
      

  4.   

    zjcxc(邹建) 你写的还是有点点问题,如果我把第一条的金额改成2000,这样sessionid=00001的amt累加就>0的了,这样虽然在union下面的语句里不能显示,但是在union上面的语句里却显示了,但实际上这条数据是不应该出来的,因为只要合计值>0就不能显示的
      

  5.   

    select * from 表 
    where rundate between '2004-07-01' and '2004-08-01'
    and alcode='00001'
    and amt>0
    union
    select * from 表 
    where alcode='00001'
    and sessionid=a.sessionid
             and amt<0
             and sheetid not in
             ( 
             select sheetid from 表 
             where rundate between '2004-07-01' and '2004-08-01'
    and alcode='00001'
    and amt>0
              )
      

  6.   

    select * from 表 a
    where rundate between '2004-07-01' and '2004-08-01'
    and alcode='00001'
    and (select sum(amt) from 表 where sessionid=a.sessionid)>0
    union
    select * from 表 a
    where alcode='00001'
    and(select sum(amt) from 表 where sessionid=a.sessionid)<0
      

  7.   

    还是不对,这样前一段sql所有的数据都出来了,我要的是amt累计<0的金额,>0了就不应该显示了就是说如果00001累计的金额>0,就应该只显示00002的那条数据,然国00001的累计金额<0,就显示00001的所有数据和00002的那条数据
      

  8.   

    我想查出的数据:1.在2004-07-01至2004-08-01这段时间内,alcode=00001,所有的amt>0的集合和2.alcode=00001, 同一sessionid同一alcode下的amt<0,但rundate无限制的集合如果同一sessionid同一alcode下的amt>0,那么这个sessionid下的所有数据都不能被列出
    但是如果要同一sessionid同一alcode下的amt>0,必定有一条数据的amt是>0的,但是这个数据不能被列出,这和第一个条件又冲突了,所以我写不来了
      

  9.   

    上面的数据,1,2,4的sessionid都是00001,他们的amt合计>0,所以这三条数据都不应该被列出
    如果他们的合计<0,那么不管rundate的范围,这三条数据都应该被列出
      

  10.   

    --示例--示例数据
    create table tb(sheetid int,amt int,sessionid varchar(10),alcode varchar(10),rundate varchar(10))
    insert tb select 1, 200,'00001','00001','2004-07-18'
    union all select 2,-400,'00001','00001','2004-07-18'
    union all select 3, 200,'00002','00001','2004-07-18'
    union all select 4, 400,'00001','00001','2004-07-18'
    union all select 5,-200,'00001','00001','2004-08-18'
    go--这个意思?
    select *
    from tb a
    where rundate between '2004-07-01' 
    and '2004-08-01' --在2004-07-01至2004-08-01这段时间内
    and alcode='00001'  
    and amt>0            --lcode=00001,所有的amt>0
    and exists(          --同一sessionid同一alcode下的amt>0
                         --这个sessionid下的所有数据都不能被列出
                     --也就是必须存在<=0的
    select * from tb
    where sessionid=a.sessionid
    and alcode=a.alcode
    and amt<=0)
    union
    select a.*
    from tb a,(
    select sessionid,alcode
    from tb
    where alcode='00001'      --alcode=00001
    group by sessionid,alcode
    having sum(amt)<0         --同一sessionid同一alcode下的amt<0
    )b where a.sessionid=b.sessionid 
    and a.alcode=b.alcode
    go--删除测试
    drop table tb/*--测试结果sheetid     amt         sessionid  alcode     rundate    
    ----------- ----------- ---------- ---------- ---------- 
    1           200         00001      00001      2004-07-18
    4           400         00001      00001      2004-07-18(所影响的行数为 2 行)
    --*/