如何计算SQL Server中的不同列

时间:2013-05-11 13:48:09

标签: sql sql-server

请帮帮我..

我运行此查询:

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

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

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

2 个答案:

答案 0 :(得分:2)

只需将现有查询用作子查询即可。顺便说一句,如果你有一个select distinct条款,则不需要group by

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

以下是SQL Fiddle

答案 1 :(得分:2)

您是否希望用于创建结果的数据库表tbltes中的行数?或结果中的行数?

如果后者只是将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