Ekta Sharma
Ekta Sharma

Reputation: 90

how to get the count of all the elements in the field?

i have one table:

id | name |
-----------
1  | AAA  |
2  | BBB  |
3  | AAA  |
4  | DDD  |
5  | BBB  |
6  | CCC  |
7  | AAA  |
8  | BBB  |
9  | DDD  |

I want to count the that how many times AAA ,BBB and so on are there. By using count(*) i am getting the hole count not for single element. I want the count of all the elements. like this:

Name | Count
------------
AAA  | 3
BBB  | 3
CCC  | 1
DDD  | 2

Not in new table i want it as output.

Upvotes: 3

Views: 653

Answers (4)

nopaws
nopaws

Reputation: 245

SELECT name AS Name,
       count(name) AS Count
FROM Table
GROUP BY name ASC;

Upvotes: 0

Sonu Yadav
Sonu Yadav

Reputation: 11

Here is the simplest way to count rows with what you want to match:

sql> select count(*) as count from table_name where name like 'AAA';

Upvotes: 0

Esha Garg
Esha Garg

Reputation: 144

Select name,Count(*) as 'total'
from yourTableName
GroupBy 

Upvotes: 0

John Woo
John Woo

Reputation: 263723

Use COUNT() aggregate function to get the total number of records for each NAME.

SELECT  name, COUNT(*) totalCOUNT
FROM    tableName
GROUP   BY name 

Upvotes: 5

Related Questions