简体   繁体   中英

How to count distinct column in SQL Server

Please help me ..

I run this query :

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

and now.. I want to count all row that showed by the query above..

I had tried this query ..

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

but not like what I expected...

So I need your help master...

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM