-1

I have got a table with ID's:

Id
===
1
2
2
3
1
2

And I want to create a table with two columns like this:

Id      COUNT
=============
1      2
2      3
3      1

How can I do that?

grooove
  • 85
  • 2
  • 8

4 Answers4

3

Let's say you called your table 'user', you can try this :

SELECT user.Id as ID, count(user.Id) as COUNT_ID
FROM user
GROUP BY ID;

Hope it helps, WaLinke

WaLinke
  • 726
  • 2
  • 6
  • 25
2

You have to group by your id.

Select id, count(1) as COUNT
from yourtable
group by ID
order by id

That way you tell your sql, that you want to count the number of rows per id.

If you need more examples feel free to google sql count. There are many good examples out there. Or check this stackoverflow question: How to use count and group by at the same select statement

BabbleGum
  • 66
  • 6
1

You can use GROUP BY and Count(columnname) functions like this

SELECT
Id,
Count(Id) AS COUNT
FROM
tablename
GROUP BY Id
Rtra
  • 514
  • 12
  • 25
1

Something like this should work

SELECT id, COUNT(id) AS Expr1 FROM dbo.Table1 GROUP BY id
Michaël Roy
  • 6,338
  • 1
  • 15
  • 19