数据表A 字段 a1,a2,a3,a4 a3是int类型 其他vachar
想把 A表中 按 a1分组,把a3不等于 0的 所有a2的 行数计算出来。。select a1,count(a2)   from A group by a1
请问这里怎么做判断 ,count(a2)  计算出 a3不等于0时,所有a2的行数?

解决方案 »

  1.   

    SELECT 
      A1,
      COUNT(DISTINCT A2) AS [A2CNT]
    FROM
      A
    WHERE 
      A3!=0
    GROUP BY 
      A1
      

  2.   

    select a1,
    count(case  when a3!=0 then a2 end) as a2
    from A 
    group by a1
      

  3.   

    select a1,count(*) from A where a3!=0 group by a1 
      

  4.   

    select a1,count(*) from A where a3<>0 group by a1
      

  5.   

    select a1,a2=count(1)  from A where a3<>0 group by a1 
      

  6.   

    select a1,
    count(case  when a3!=0 then a2 end) as a2
    from A 
    group by a1
      

  7.   

    select a1,a2=sum(case when a3<>0 then 1 else 0 end) 
     from A 
    group by a1 
      

  8.   

    select a1,a2=sum(case when a3<>0 then 1 else 0 end) 
     from A 
    group by a1 
      

  9.   


     select a1  ,a2行=COUNT(a2)  from @A where a3<>0 group by a1
      

  10.   

    select a1,sum(case when a3<>0 then 1 else 0 end) from A group by a1 
      

  11.   


     select a1  ,a2行=COUNT(a2)  from @A where a3<>0 group by a1
      

  12.   

    declare @table table (a1 varchar(1),a2 varchar(1),a3 int,a4 varchar(1))
    insert into @table
    select 'a','b',1,'d' union all
    select 'j','h',0,'d' union all
    select 'u','j',9,'j'select a1,a2=sum(case when a3<>0 then 1 else 0 end) 
    from @table group by a1
    /*
    a1   a2
    ---- -----------
    a    1
    j    0
    u    1
    */
      

  13.   

    select a1,count(a2) A2 from A where a3 != 0 group by a1
      

  14.   

    推荐:select a1,count(*) from A where a3!=0 group by a1 
    麦蒂慢了..
      

  15.   

    select a1,count(a2) A2 from A where a3 != 0 group by a1
      

  16.   

    select a1,count(a2) A2 from A where a3 != 0 group by a1
      

  17.   

    select a1,count(a2) A2 from A where a3 != 0 group by a1
      

  18.   

    select a1,count(a2) A2 from A where a3 != 0 group by a1
      

  19.   

    select a1,sum(decode(a3,0,0,1)) from A group by a1;
      

  20.   

    select a1,计数=sum(case when a3<>0 then 1 end)  from A group by a1 
      

  21.   


    select a1,count(*) from A where a3<>0 group by a1 
      

  22.   


    select a1,sum(case when a3<>0 then 1 else 0 end) as a2
     from A 
    group by a1