计算行项目的单次出现次数

时间:2013-07-31 09:58:50

标签: sql count

我想计算一列中的项目只出现一次的次数。例如,如果在我的表中我有......

Name
----------
Fred
Barney   
Wilma
Fred
Betty
Barney
Fred

...它会让我数为2,因为只有Wilma和Betty出现过一次。

4 个答案:

答案 0 :(得分:2)

   select count(*) from
      (select count(*) from Table1
       group by Name
       having count(*) =1) s

SqlFiddle

答案 1 :(得分:2)

<强> Here is SQLFiddel Demo

以下是您可以尝试的查询:

select count(*) from 
(select Name 
  from Table1
 group by Name
having count(*) = 1) T

直到我的帖子上面是你的实际帖子。

<小时/> 以下是修改后问题的帖子:

在oracle中,您可以尝试以下查询:

select sum(count(rownum))
  from Table1
 group by "Name"
having count(*) = 1

OR

<强> Here is SQLFiddel Demo

<小时/> 在SQL Server中,您可以尝试以下查询:

SELECT COUNT(*) 
  FROM Table1 a
  LEFT JOIN Table1 b 
    ON a.Name=b.Name 
   AND a.%%physloc%% <> b.%%physloc%%
 WHERE b.Name IS NULL

OR

<强> Here is the SQLFiddel Demo


在Sybase中,您可以尝试以下查询:

select count(count(name)) 
  from table 
 group by name 
having count(name) = 1

根据@ user2617962的回答。

谢谢

答案 2 :(得分:0)

由于您只需要出现一次没有列实际值的列值计数,查询应为:

select count(count(name)) from table group by name having count(name)
= 1

答案 3 :(得分:0)

尝试以下操作。

select name from (select name, count(name) as num from tblUsers group by name)
tblTemp where tblTemp.num=1

标记它是否有效..:)