Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding difference between total value and distinct value in a table

Input Table: City

  +-----------+
  |   Name    |
  +-----------+
  |   AAA     |
  +-----------+
  |   AAA     |
  +-----------+
  |   BBB     |
  +-----------+
  |   CCC     |
  +-----------+

The table City has one column Name. I want output that will return the difference between total numbers of city name and distinct number of city name.

In case of this table the result would be 1.

like image 889
BlackCat Avatar asked Dec 14 '22 04:12

BlackCat


2 Answers

Use COUNT and DISTINCT COUNT.

SELECT COUNT(name)-COUNT(DISTINCT name) AS nameDifference
FROM city

Output:

nameDifference
1
like image 172
Matt Avatar answered Dec 29 '22 00:12

Matt


Try this

SELECT COUNT(Name)-COUNT(DISTINCT Name) AS NewName
FROM city
like image 37
Manish Kumar Avatar answered Dec 29 '22 00:12

Manish Kumar