一句select里面有两个count(person)
但是两个count(person)的条件不一样(where条件不一样)
怎么合在一句SQL里面?

解决方案 »

  1.   

    select count(person) from 表 where 条件1
    union all
    select count(person) from 表 where 条件2
      

  2.   

    select a.pk,a.cnt,b.cnt from 
    (select pk, count() cnt……from where group by)a,
    (select pk,count() cnt……from where group by) b
    where a.pk=b.pk
    --pk是标记字段,例如你要查人数的范围,比如部门号,班级号之类的
      

  3.   

    select s.cdtxt,t.streetname,count()
    from social.grantdetail g
    inner join sqdta.sqcode s on g.branch1=s.cdcode
    where s.cdtype='STREET.BRANCHID' and怎么将这两个合进去?
    select count(person) from 表 where 条件1
    union all
    select count(person) from 表 where 条件2
      

  4.   

    把where 放宽一点然后用case when 可以吗?
    select sum(case when 条件1 then 1 else 0) person1,sum(case when 条件2 then 1 else 0) person2 from table where 条件1 or 条件2
      

  5.   

    select sum(case when 条件1 then 1 else 0 end) person1,sum(case when 条件2 then 1 else 0 end) person2 from table where 条件1 or 条件2
      

  6.   

    select s.cdtxt,t.streetname,count()
    from social.grantdetail g
    inner join sqdta.sqcode s on g.branch1=s.cdcode
    where s.cdtype='STREET.BRANCHID' and怎么将这两个合进去?
    select count(person) from 表 where 条件1
    union all
    select count(person) from 表 where 条件2