简体   繁体   English

如何计算SQL Server中的不同列

[英]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. 顺便说一句,如果你有一个group by子句,则不需要select distinct

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 这是SQL小提琴

Do you want the number of rows in database table tbltes used to create the results ? 您是否希望用于创建结果的数据库表tbltes中的行数? or the number of rows in results ? 或结果中的行数?

If the latter just put Select Count(*) From around the whole thing... 如果后者只是将Select Count(*) From放在整个事物的周围......

 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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