现在我有一个表,有很多字段,要根据各字段不同的条件统计各个区域的数量。table:
areaid   a    b    c        d    e       f 
5201     10   0    false    8    0101    01
520102   1    1    true     5    0201    03
520103   4    9    fale     10   2001    04
520104   100  20   true     20   0201    03查询条件:
1、统计各个区a>=10的
2、统计各个区b在 0到20 之间
3、统计各区c 为true的
4、统计各区e字段为0201的
5、统计各区f字段为03的目前我的统计方法是:
1.select areaid ,count(*) from table where a>10 group by areaid 
2,select areaid ,count(*) from table where b between 0 and 20 group by areaid 
3,select areaid ,count(*) from table where c=true group by areaid
4,select areaid,count(*) from table where e=0201 group by areaid
5,select areaid,count(*) from table where d=03 group by areaid然后把这些记录拼起来得到如下:
areaid a1 b1 c1 e1 f1 
5201 1 0 0 0 0
520102 0 1 1 1 1
520103 0 1 0 0 0
520104 1  0 1 1 1
实际上所涉及的字段很多,记录集也很庞大,如果按照我这个方法要查询table无数次磁能得到结果,效率非常低,所以想请问各位有什么好方法么? 可以提高效率又可以简化查询

解决方案 »

  1.   

    select areaid ,count(*) from table where a>10 or b between 0 and 20 or c='true' or e=0201 or d='03' group by areaid 
      

  2.   

    可以拖到多维数据集里面来处理不?让OLAP来出报表,再过滤。嘿嘿
      

  3.   

    理解错了,应该如下:
    select areaid,
           sum(case when a>10 then 1 else 0 end) a1,
           sum(case when b between 0 and 20 then 1 else 0 end) b1,
           sum(case when c='true' then 1 else 0 end) c1,
           sum(case when d='03' then 1 else 0 end) d1,
           sum(case when e=0201 then 1 else 0 end) e1
    from table
    group by areaid 
      

  4.   

    create table tb(areaid varchar(10),a int,b int, c varchar(10),d int, e varchar(10),f varchar(10))
    insert into tb values('5201'  , 10  ,0 , 'false' ,8  ,'0101', '01')
    insert into tb values('520102', 1   ,1 , 'true'  ,5  ,'0201', '03')
    insert into tb values('520103', 4   ,9 , 'fale'  ,10 ,'2001', '04')
    insert into tb values('520104', 100 ,20, 'true'  ,20 ,'0201', '03')
    goselect areaid,
           sum(case when a>10 then 1 else 0 end) a1,
           sum(case when b between 0 and 20 then 1 else 0 end) b1,
           sum(case when c='true' then 1 else 0 end) c1,
           sum(case when e='0201' then 1 else 0 end) e1,
           sum(case when f='03' then 1 else 0 end) f1
    from tb
    group by areaid drop table tb/*
    areaid     a1          b1          c1          e1          f1          
    ---------- ----------- ----------- ----------- ----------- ----------- 
    5201       0           1           0           0           0
    520102     0           1           1           1           1
    520103     0           1           0           0           0
    520104     1           1           1           1           1(所影响的行数为 4 行)
    */
      

  5.   


    select areaid,
           sum(case when a>10 then 1 else 0 end) a1,
           sum(case when b between 0 and 20 then 1 else 0 end) b1,
           sum(case when c='true' then 1 else 0 end) c1,
           sum(case when e=0201 then 1 else 0 end) e1,
           sum(case when f=03 then 1 else 0 end) f1
    from table
    group by areaid 
      

  6.   

    关注。建议:能否给where筛选的部分添加字段XXX,用来定义各类型。
    这样再添加非聚集索引areaid ,字段XXX ,条件。查询如下:
    select areaid,字段XXX,count(*) 
    from table 
    where 条件
    group by areaid ,字段XXX 以上部分胡说的,别信。
      

  7.   

    需要注意的是count(*) --包含NULL值。
      

  8.   

    一样:select areaid,
           sum(case when a>10 and 另外的条件 then 1 else 0 end) a1,
           sum(case when b between 0 and 20 另外的条件 then 1 else 0 end) b1,
           sum(case when c='true' or 另外的条件 then 1 else 0 end) c1,
           sum(case when e='0201' or 另外的条件 then 1 else 0 end) e1,
           sum(case when f='03' and 另外的条件 then 1 else 0 end) f1
    from tb
    group by areaid 至于如何组合就看你的需求了.
      

  9.   

    有点像行转列。
    sum统计函数与group by结合,与case的条件