Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

... where count(col) > 1

Tags:

I have a table like this:

+-----+-----+-------+ | id  | fk  | value | +-----+-----+-------+ | 0   | 1   | peter | | 1   | 1   | josh  | | 3   | 2   | marc  | | ... | ... | ...   | 

I'd like now to get all entries which have more than one value. The expected result would be:

+-----+-------+ | fk  | count | +-----+-------+ | 1   | 2     | | ... | ...   | 

I tried to achieve that like this:

select fk, count(value) from table where count(value) > 1; 

But Oracle didn't like it.

So I tried this...

select * from (     select fk, count(value) as cnt from table ) where cnt > 1; 

...with no success.

Any ideas?

like image 541
cimnine Avatar asked Nov 26 '09 16:11

cimnine


People also ask

Can you use COUNT in WHERE clause?

SQL SELECT COUNT() can be clubbed with SQL WHERE clause.

How do you use COUNT 1?

In other words, COUNT(1) assigns the value from the parentheses (number 1, in this case) to every row in the table, then the same function counts how many times the value in the parenthesis (1, in our case) has been assigned; naturally, this will always be equal to the number of rows in the table.


1 Answers

Use the having clause for comparing aggregates.

Also, you need to group by what you're aggregating against for the query to work correctly. The following is a start, but since you're missing a group by clause still it won't quite work. What exactly are you trying to count?

select fk, count(value)  from table  group by fk having count(value) > 1; 
like image 134
Donnie Avatar answered Sep 18 '22 05:09

Donnie