-1

So, let's say we have My_column which has 2 occurrances of b_value, 3 occurances of a_value and 1 occurrance of c_value, like this:

b_value
b_value
a_value
a_value
a_value
c_value

What I'd like to do is use GROUP BY on the values and then count the number of occurences and set them up accordingly, like this:

b_value | 2
a_value | 3
c_value | 1

I don't even know where to start. I'vee been fuzzing around in management studio, but no luck thus far.

4

2 回答 2

3

您需要使用group by子句和count功能如下

select My_column, count(1) as cnt
from my_table
group by My_column

Sql Fiddle 演示

于 2013-08-02T11:39:23.943 回答
1

您可以通过以下方式尝试COUNT()该分组的功能My_Column

SELECT My_Column, COUNT(*)
FROM MyTable
GROUP BY My_Column

看看这个 SQLFiddle

于 2013-08-02T11:39:38.633 回答