日期:2014-05-19  浏览次数:20654 次

初学者,有个sql语句写不出来,请帮忙看看
假设我在给别人发钞票。每次来人就给一元。
建表send,只有一个字段,id   表示每个人的id。

发了几天终于发完了,现在我想统计来领了1次的人有多少,领了两次的人有多少,领了5次的人有多少???
该怎么写呢   ????????????????????????

------解决方案--------------------
select id,count(1) as 次数 from tablename group by id
------解决方案--------------------
select 次数,count(1) from (select id,count(1) as 次数 from tablename group by id) t group by 次数
------解决方案--------------------
select count(*) from send group by id having count(*)=1 or having count(*)=2 or having count(*)=5

------解决方案--------------------
select id,count(1) as 次数 from tablename group by id

------解决方案--------------------
select id,count(*) as 次数
from tb group by id
having count(*)=?

------解决方案--------------------
Create Table TEST(ID Int)
Insert TEST Select 1
Union All Select 2
Union All Select 3
Union All Select 2
Union All Select 3
Union All Select 1
Union All Select 5
Union All Select 3
Union All Select 7
GO
Select 次數, Count(*) As 人數 From
(
Select ID, Count(*) As 次數 From TEST Group By ID) A
Group By 次數
Order By 次數
Go
Drop Table TEST
/*
次數 人數
1 2
2 2
3 1
*/