表结构如下:
  DATE        DATE not null
  ENTITYTYPE    VARCHAR2(10) not null
  ENTITYNAME    VARCHAR2(20) not null我需要做个统计,从前台传来两个值ENTITYTYPE ,ENTITYNAME
ENTITYTYPE 如果ENTITYTYPE 值为0的话,我就要检索所有ENTITYTYPE ,同样的道理
如果ENTITYNAME值为0的话,就要检索所有ENTITYNAME.也就是有4种组合.第一种  ENTITYTYPE  = 0   ENTITYNAME=0
第二种  ENTITYTYPE  = 0   ENTITYNAME=(具体的值)
第三种  ENTITYTYPE  = (具体的值)  ENTITYNAME=0
第四种  ENTITYTYPE  = (具体的值)  ENTITYNAME=(具体的值)需要的结果是:
ENTITYTYPE  ENTITYNAME count数量第三种 select count(*),ENTITYTYPE, ENTITYNAME   from table where ENTITYTYPE  =(具体的值) group by ENTITYNAME    
第四种 select count(*),ENTITYTYPE, ENTITYNAME   from table where ENTITYTYPE  =(具体的值) and ENTITYNAME=(具体的值) 第一种 和 第二种
不知道sql该怎么写了谢谢

解决方案 »

  1.   

    晕,第二种应该是这样写
    select count(*),ENTITYTYPE, ENTITYNAME from table where ENTITYNAME =(具体的值) group by ENTITYTYPE 
      

  2.   

    条件 可以用 case when  或者说是 decode 函数  
      

  3.   

    select count(*),ENTITYTYPE, ENTITYNAME from table  group by ENTITYTYPE,ENTITYNAME;
      

  4.   


    create or replace procedure get_entity_count
    (entity_cur out sys_refcursor, entity_type varchar2:='0', entity_name varchar2:='0')
    is
     stmt varchar2(4000):='select entityType,entityName,count(*) cnt from tb where 1=1 ';
    begin
     stmt:=stmt ||
      (case entity_type when '0' then '' else ' and entityType='''||entity_type||'''' end) ||
      (case entity_name when '0' then '' else ' and entityName='''||entity_name||'''' end) ||
      ' group by entityType,entityName';-- dbms_output.put_line(stmt);
     open entity_cur for stmt;
    end;
    /
      

  5.   


    晕菜,原来group by 后面可以跟多个.
    请问,这样效率怎么样.
      

  6.   


    group by 主要涉及到数据库的排序操作,相对较耗性能,数据量不大的话,还是很快的。
    并且实际需要的话,也是没办法。