select count(t.d502_00)
  from d502 t
 where t.d502_12 = '10000000000186'
   and t.d502_57 >= to_date('2011-07-01', 'yyyy-mm-dd')
   and t.d502_57 <= to_date('2011-07-10', 'yyyy-mm-dd')
   and t.d502_60 = '1'
   and t.d502_61 = '1'结果是:50
   这个是正确的,但再添加一个条件之后就不正确了
select count(t.d502_00),sum(s.d504_09)
  from d502 t,d504 s
 where t.d502_12 = '10000000000186'
   and t.d502_57 >= to_date('2011-07-01', 'yyyy-mm-dd')
   and t.d502_57 <= to_date('2011-07-10', 'yyyy-mm-dd')
   and t.d502_60 = '1'
   and t.d502_61 = '1'
   and t.d502_00 = s.d504_04结果:14441      444731.03
为什么会影响count(t.d502_00)的值呢? 而且现在取出的“14441”是符合条件的 d504 表中的行数。唉~~~头疼

解决方案 »

  1.   

    and t.d502_00 = s.d504_04
    这个条件,2个表关联条件,对结果肯定有影响了
      

  2.   

    select count(t.d502_00),sum(s.d504_09)
      from d502 t,d504 s
     where t.d502_12 = '10000000000186'
      and t.d502_57 >= to_date('2011-07-01', 'yyyy-mm-dd')
      and t.d502_57 <= to_date('2011-07-10', 'yyyy-mm-dd')
      and t.d502_60 = '1'
      and t.d502_61 = '1'
      and t.d502_00 = s.d504_04(+)----------这里加一个左连接。
      

  3.   

    先别汇总,看出来时什么结果select t.d502_00,s.d504_09
      from d502 t,d504 s
     where t.d502_12 = '10000000000186'
      and t.d502_57 >= to_date('2011-07-01', 'yyyy-mm-dd')
      and t.d502_57 <= to_date('2011-07-10', 'yyyy-mm-dd')
      and t.d502_60 = '1'
      and t.d502_61 = '1'
      and t.d502_00 = s.d504_04(+)----------这里加一个左连接。
      

  4.   

    楼主,你这个需求不能这么写,你第二个查询,select的结果是两个表关联的结果,也就是说你count里面不论你写了哪个字段,查到的结果都是关联后的结果。
      

  5.   

    这样吧:
    select (select count(tt.d502_00)
      from d502 tt
     where tt.d502_12 = '10000000000186'
      and tt.d502_57 >= to_date('2011-07-01', 'yyyy-mm-dd')
      and tt.d502_57 <= to_date('2011-07-10', 'yyyy-mm-dd')
      and tt.d502_60 = '1'
      and tt.d502_61 = '1')cnt,sum(s.d504_09)
      from d502 t,d504 s
     where t.d502_12 = '10000000000186'
      and t.d502_57 >= to_date('2011-07-01', 'yyyy-mm-dd')
      and t.d502_57 <= to_date('2011-07-10', 'yyyy-mm-dd')
      and t.d502_60 = '1'
      and t.d502_61 = '1'
      and t.d502_00 = s.d504_04
      

  6.   


    with aa
    as
    (
    select t.d502_00,s.d504_09,id
      from d502 t,d504 s
     where t.d502_12 = '10000000000186'
      and t.d502_57 >= to_date('2011-07-01', 'yyyy-mm-dd')
      and t.d502_57 <= to_date('2011-07-10', 'yyyy-mm-dd')
      and t.d502_60 = '1'
      and t.d502_61 = '1'
      and t.d502_00 = s.d504_04(+)----------这里加一个左连接。
    )select count(d502_00),sum(s.d504_09) from aa group by id