读书人

求一条 SQL 语句,多谢

发布时间: 2012-02-19 19:43:38 作者: rapoo

求一条 SQL 语句,谢谢
某张表中存在用户字段 user_id,及对应金额字段 user_Fee 其数值为
user_id user_Fee
001 85.37
002 108.43
003 364.25
001 207.56
... ...

现在想查询出所有100元分段为分组的用户数,如:
0-100元: 30个用户
100-200元: 20个用户
.....

请问怎样实现,谢谢

[解决办法]
declare @t table(user_id varchar(10),user_free float)
insert into @t select '001 ',85.37
union all select '002 ',108.43
union all select '003 ',364.25
union all select '001 ',207.56

select LTRIM(FLOOR (user_free/100)*100)+ '- '+LTRIM((FLOOR (user_free/100)+1)*100)+ '元 ',LTRIM(count(*))+ '个用户 '
from @t
group by LTRIM(FLOOR (user_free/100)*100)+ '- '+LTRIM((FLOOR (user_free/100)+1)*100)+ '元 '

/*
0-100元 1个用户
100-200元 1个用户
200-300元 1个用户
300-400元 1个用户

(所影响的行数为 4 行)

*/

读书人网 >SQL Server

热点推荐