Get the number of values ​​that appear only once in a column

Firstly, if it is relevant, I use MySQL, although I assume that the solution will work through database products. My problem is this:

I have a simple table with one column. There are no restrictions for the column. There are some simple data inside this column, for example

a
a
b
c
d
d

I need to get the number / number of values ​​that appears only once. From the above example, there will be 2 (since only b and c occur once in the column).

Hope it is clear that I do not want the DISTINCT values, but UNIQUE. I did this earlier by creating an extra table with a UNIQUE constraint for the column and just INSERTing to a new table from the old one, processing duplicates accordingly.

I was hoping to find a solution that did not require a temporary table, and somehow it could just be done with an excellent SELECT.

+5
source share
5 answers

Assuming your table is called Tand your field is called F:

SELECT COUNT(F)
FROM (
    SELECT F
    FROM T
    GROUP BY F
    HAVING COUNT(*) = 1
) AS ONLY_ONCE
+14
source
select field1, count(field1) from my_table group by field1 having count(field1) = 1

select count(*) from (select field1, count(field1) from my_table group by field1 having count(field1) = 1)

the first returns those that are unique, and the second returns the number of unique elements.

+3
source
select count(*) from 
(
  select
    col1, count(*)
  from 
    Table
  group by 
    Col1
  Having 
    Count(Col1) = 1
)
+3

:

Select MyColumn From MyTable Group By MyColumn Where Count(MyColumn) = 1
+3

...

select count( cnt ) from
( select count(mycol) cnt from mytab group by mycol )
where cnt = 1
+3

All Articles