set nocount on
go
Create table #tmp (name varchar(3),X int,Y int)
go
Insert into #tmp values ('a',1,3)
Insert into #tmp values ('b',0,0)
Insert into #tmp values ('c',0,2)
Insert into #tmp values ('a',1,4)
Insert into #tmp values ('b',1,3)
Insert into #tmp values ('c',0,0)
Insert into #tmp values ('b',1,2)
go
--例子是否有錯?Y值不為0的比例如果得小於60%的話就沒記錄了。(例子中是小於70%的,道理一樣)Select  distinct b.name  From (
Select name From 
(Select name,Sum(case when y=0 then 0 else 1 end) as cnt,count(*) as p,sum(Y) as Pavg   From 
#tmp  tmp group by name ) as ss
where cnt*1.0 /p<0.7 and Pavg*1.0 /cnt >=2) a  INNER JOIN #tmp  b ON a.name=b.name 
where b.x=1
Drop table #tmp 

解决方案 »

  1.   

    我不是按钮,可以做吗?select name from ( 
    select name, avg(y) a,avg (case y when 0 then 0 else 1 end) b from table1 where x=1 group by name 
    )as n where n.a >2 and n.b >0.6
      

  2.   

    sorry,错了,没注意(平均值计算时不包括y=0的情况);
      

  3.   

    select distinct t1.name 
      from mytable t1,
           (select name, sum(y) sumY, sum(decode(y,0,0,1)) countYnot0,count(*) countY
              from mytable
             group by name) t2
     where t1.x=t2.x
       and t1.x=1
       and countYnot0 < countY * 0.6
       and sumY >= countY * 2
    /不要用除法判断
      

  4.   

    select distinct name 
      from mytable t1,
           (select name, sum(y) sumY, sum(decode(y,0,0,1)) countYnot0,count(*) countY
              from mytable
             group by name) t2
     where t1.name=t2.name  --刚才写错了
       and t1.x=1
       and countYnot0 < countY * 0.6
       and sumY >= countY * 2
    /不要用除法判断
      

  5.   

    Create table tmp (name varchar(3),X int,Y int)
    go
    Insert into tmp values ('a',1,3)
    Insert into tmp values ('b',0,0)
    Insert into tmp values ('c',0,2)
    Insert into tmp values ('a',1,4)
    Insert into tmp values ('b',1,3)
    Insert into tmp values ('c',0,0)
    Insert into tmp values ('b',1,2) select distinct name from tmp t where 
        (select count(*) from tmp where
     x=1 and y<>0 and t.name=name)/(select count(*) from tmp where 
    t.name=name)>0.6
    and
    (select avg(y) from tmp where t.y=y)>2
      

  6.   

    错了, 把Y=0也计算进平均值了!
    条件也打错了!
    select distinct name from tmp t where 
        (select count(*) from tmp where
     x=1 and y<>0 and t.name=name)/(select count(*) from tmp where 
    t.name=name)>0.6
    and
    (select avg(y) from tmp where t.name=name and y<>0)>2
      

  7.   

    select distinct name 
      from mytable t1,
          (select name, sum(y) sumY, sum(decode(y,0,0,1)) countYnot0,count(*) countY
              from mytable
            group by name) t2
    where t1.name=t2.name  --刚才写错了
      and t1.x=1
      and countYnot0 < countY * 0.6
      and sumY >= countYnot0 * 2  --经按钮同志提醒,再次改正
      

  8.   

    To:KingSunSha(弱水三千)
    为什么"不要用除法判断"??
    select name from 
        (select sum(case y when 0 then 0 else 1 end) as ocount,count(y) as ycount,avg(y) as acount,name from table1 group by [name]) as xtable
        where acount>=2 and ocount/ycount<0.6
      

  9.   

    我猜弱水的意思可能是怕有小數位的差異罷?
    SQL Server中
    Select 1/3 結果是0
    Select 1*1.0/3 結果是0.333333
      

  10.   

    to KingSunSha(弱水三千):在Sqlserver7.0中,报错 'decode' is not a recognized function name
      

  11.   

    to yangtom() :avg(y) 不对,因为平均值不包括y=0的情况,如果当初设计表时,把0换成null就可以。
      

  12.   

    我参考rwq_(风云浪子) 的答案,用下列语句似乎解决了(因为答案是正确的)
    select distinct name from table1 t where 
        (select count(*) from table1 where
         x=1 and y<>0 and t.name=name)/(select count(*) from table1 where 
        t.name=name)<=0.6    
        and    
        (select avg(y) from table1 where t.name=name and y<>0 )>=2
    and x=1
    请大家看看有无问题!!谢谢
      

  13.   

    KingSunSha(弱水三千)兄的是在ORACLE下面实现的,我的在SQL SERVER实现的,我的效率比较上面几位兄弟的是比较低!如果数据不多没有什么大问题,数据太多最好不要用!
      

  14.   

    KingSunSha(弱水三千)兄的是在ORACLE下面实现的,我的在SQL SERVER实现的,我的效率比较上面几位兄弟的是比较低!如果数据不多没有什么大问题,数据太多最好不要用!
      

  15.   

    to N_chow(一劍飄香++):
    X,Y字段的数据类型是float的话,就没有小数点的问题 
      

  16.   

    我指的是Count(*)出來的結果是int型
      

  17.   

    select distinct name from table1 t where 
        (select count(*) from table1 where
        x=1 and y<>0 and t.name=name)/(select count(*) from table1 where 
        t.name=name)<=0.6    
        and    
        (select avg(y) from table1 where t.name=name and y<>0 )>=2
    and x=1
    有错:
    改为:
    select distinct name from table1 t where 
        convert(float,(select count(*) from table1 where
        x=1 and y<>0 and t.name=name))/(select count(*) from table1 where 
        t.name=name)<=0.6    
        and    
        (select avg(y) from table1 where t.name=name and y<>0 )>=2
    and x=1
      

  18.   

    最笨的办法:
    select distinct name from table1 where x =1 and name in (
    select name  from table1 where y<>0   group by name having avg(convert(float,y)) >2 
    )  
    and name in ( 
    select name  from table1 group by name having avg(convert(float,case y when 0 then 0 else 1 end)) < 0.7
    )
      

  19.   

    Delphi问题:我基于Panel组件做了一个ActiveX控件(注意不是ActiveForm,也不是VCL组件),但是新的控件没有MouseMove和MouseDown事件,请问各位高手,如何增加上这个事件,最好有详细的步骤说明,另见http://www.csdn.net/expert/topic/482/482855.shtm和http://www.csdn.net/expert/topic/494/494278.shtm,上还有100分,如能解决立即这100分相送,另外再加送200分。Delphi问题