日期:2014-05-17  浏览次数:20454 次

sql 查询语句求助
数据为:
编号    时间          厂家     
1       2000.1.1        a       
1                       a 
1                       a 
2                       a 
2                       a 
3       2000.1.1        a
---------------------------- 
4                       b 
4                       b 
5       2000.1.1        b
-------------------------- 
6       2000.1.1        c 
6       2000.1.1        c

要求按厂家分组统计,问题列 为编号去重统计个数,未处理列 当时间为空值时统计不重复编号的个数。统计结果如下:
  厂家   问题   未处理
  a      3       2
  b      2       1
  c      1       0 

------解决方案--------------------
select 厂家, count(distinct 编号),count(distinct case when 时间='' then 编号 else null end)
from tb group by 厂家
------解决方案--------------------
select a.厂家,b.num 问题 ,isnull(c.num,0)  未处理
from (select distinct 厂家 from tb ) a 
left join 
(select 厂家,count(1)num from (select distinct * from tb)
t group by 厂家) b on a.厂家 = b.厂家
left join 
(select 厂家,count(1) num from (select distinct * from tb)
t where 时间 is  null group by 厂家) c
on a.厂家 = c.厂家