id(自增)   字段1   字段2   字段3      字段4   字段5  
1        A    a      2007-01-01    完成        1
2             A        b      2007-01-01  完成     1
3        A     c      2007-01-01    完成        1
4             A        d      2007-01-01    完成        1
5             A        null   2007-01-02    末完成    2
6             B        a      2007-01-03    完成        3 
求一第SQL语句
要求按字段1分组同时对字段2进行count操作当字段2为null的时候oucnt结果显示成0(当字段2的值为null的时候,可以保证对应该的字段5的值是唯一的).
当分组记录中字段5有两个以上不同的值的时候将记录显示出来(如字段1分组时字段5中含有1,2两个值显示出两条记录,有3个不同值时显示3条记录),如只有一个值不显示,

解决方案 »

  1.   

    --try 
     Select 字段1, 字段2,decode(字段2,1,NULL,字段5) as 字段5
     from (
     Select 字段1,sum(case when 字段2 is not null 
               then 1 else 0 end) as 字段2,  字段5
      from 表 group by 字段1,字段5) t
      

  2.   

    我想的结果就是这样的!id(自增)   字段1   字段2    
    1        A    4              
    2             A        0这样就可以了!           
      

  3.   

    Select Rownum as ID, 字段1,decode(字段2,1,0,字段5) as 字段2
     from (
     Select 字段1,sum(case when 字段2 is not null 
               then 1 else 0 end) as 字段2,  字段5
      from 表 group by 字段1,字段5
      having sum(case when 字段2 is not null 
               then 1 else 0 end)>1) t