我需要统计表中满足某条件的记录个数。希望语句是这样的:Select id, Count(a=1 and b=2),count(c=3 and d=4) from table group by iddecode函数一次只能判断一个条件啊,那上面要求的语句该怎么写哦?

解决方案 »

  1.   

    select t1.a,t2.b from 
    (select count(*) a 
    from table group by id
    where a=1 and b=2
    )t1,
    (select count(*) b
    from table group by id 
    where c=3 and d=4
    )t2
      

  2.   

    Select id, 
    sum(case when a=1 and b=2 then 1 else 0 end),
    sum(case when c=3 and d=4 then 1 else 0 end)
    from table group by id 
      

  3.   

    发错了,应该用case when 语句
    case when 条件1 then 结果1
          when 条件2 then 结果2
    else
        结果3
    end
      

  4.   

    select 
    id,
    sum(case 
      when a=1 and b=2 then 1else 0 
    end),
    sum(case 
      when c=3 and c=4 then 1else 0 
    end),
    from table
    group by id