2

请帮我 ..

我运行这个查询:

select 
    distinct barang, COUNT(*) as jumlah, CAST(COUNT(*) as float) / 6 
from  
    tbltes  
group by 
    barang 
Having 
    CAST(COUNT(*) as float) / 6 >0.2

现在..我想计算上面查询显示的所有行..

我试过这个查询..

 select 
     count (distinct barang)    
 from 
     tbltes 
 group by 
     barang  
 having 
     CAST(COUNT(*) as float) /6 > 0.2

但不像我预期的那样......

所以我需要你的帮助主人...

4

2 回答 2

3

Just use your existing query as a sub-query. By the way, there is no need for select distinct if yo have a group by clause.

select count(distinct sq.barang)
from
(
select  barang as barang
        ,COUNT(*) as jumlah
from    tbltes  
group by 
        barang 
) sq
where cast(sq.jumlah as float)/6 > 0.2

Here is the SQL Fiddle

于 2013-05-11T13:58:30.827 回答
2

Do you want the number of rows in database table tbltes used to create the results ? or the number of rows in results ?

If the latter just put Select Count(*) From around the whole thing...

 Select Count(*)
 From  (select distinct barang,COUNT(*) as jumlah,
            CAST(COUNT(*) as float) / 6 
        from  tbltes  
        group by barang 
        Having CAST(COUNT(*) as float) / 6 >0.2 ) z
于 2013-05-11T14:00:07.223 回答