Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply Count on multiple distinct columns and use Having clause

I would like to do something like this , but getting an error please suggest some good methods?

select A,B,C, count(Distinct A,B,C)
from table_name 
group by A,B,C 
having count(Distinct A,B,C) > 1 

Basically i have an index on the columns(A,B,C), and some rows doesnt have this unique combination set, So I'm trying a query similar to identify the rows which disobeys the unique constraint. PLease let me know if there is a best way

like image 955
amateur Avatar asked Jun 02 '14 22:06

amateur


1 Answers

If you group by these columns then you already only get those unique records and then you can use count(*) to get how many duplicates you have

select A,B,C, count(*) 
from table_name 
group by A,B,C 
HAVING count(*) > 1
like image 137
juergen d Avatar answered Nov 15 '22 04:11

juergen d