zhanzezhu
zhanzezhu

Reputation: 121

SQL:How to count record which distinct?

The data source:

id  idx num
1   1   1
2   1   2
3   2   3
4   2   3
5   3   4
6   3   4
7   3   5

I need count record which the same idx and count num's type.

I hope result:

idx num
1   2
2   1
3   2

Upvotes: 0

Views: 46

Answers (1)

Giorgos Betsos
Giorgos Betsos

Reputation: 72175

What you want is a DISTINCT count:

SELECT idx, COUNT(DISTINCT num) AS num
FROM mytable
GROUP BY idx

Upvotes: 3

Related Questions