-2

我有一张这样的桌子:

staffname | periods
___________________
david     |     yes
seema     |     no
raj       |     yes
seema     |     no
david     |     yes

现在我想要这样的结果:

staffname | periods
___________________
david     |     2
seema     |     2
raj       |     1

如果您能理解我想要名称和计数,则第二列的值无关紧要。

我已经尝试过,但我得到了重复的行。

请帮忙。

4

5 回答 5

3

尝试这个:

SELECT STAFFNAME, COUNT(*) as PERIODS
   FROM TABLE
 GROUP BY STAFFNAME
于 2013-06-15T12:16:15.043 回答
1

尝试这个

   select staffname, count(periods) as periods from table1 
   group by staffname

如果您想像结果一样订购它们,只需在最后添加

    order by periods desc

演示在这里

于 2013-06-15T12:16:21.937 回答
0

试试下面的代码。它可以提供您想要的准确结果。

select staffname, count(1) as 'periods' from table1
group by staffname order by periods desc, staffname;

链接到sqlfiddle

于 2013-06-15T12:24:10.540 回答
0
    SELECT staffname, COUNT(*) AS periods 
    FROM first_table 
    GROUP BY staffname
    ORDER BY periods DESC
于 2013-06-15T12:26:07.103 回答
0

这很简单,

SELECT staffname,COUNT(periods) AS periods
FROM TableName
Group By staffname 
于 2013-06-15T12:29:46.220 回答