1

i have ASP website with SQL database table. in this table column mane "type". i want to get all distinct values from this column with values count in datatable. for example for database table:

id  type
---------
1   type1
2   type2
3   type3
4   type2
5   type2
6   type3

i want to get the following datatable:

type  count
------------
type1   1
type2   3
type3   2
user281812
  • 195
  • 3
  • 14

3 Answers3

3

You can use

SELECT type, COUNT(type) as count
FROM Table1 
GROUP BY type

Result

|  type | count |
|-------|-------|
| type1 |     1 |
| type2 |     3 |
| type3 |     2 |

SqlFiddle DEMO

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
0

I would suggest that you do this in sql itself by using following query

SELECT type, COUNT(type) as count
FROM myTableName 
GROUP BY type
Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
0

SQL

SELECT type, COUNT(type) as count
FROM Table1 
GROUP BY type

Linq

from t in <YourEntityObject>.YourTableName 
group t by t.type into myVar 
select new
   {
         k = myVar.Key,
         c = myVar.Count()
   };
Abdulhakim Zeinu
  • 3,333
  • 1
  • 30
  • 37