某张表中存在用户字段 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个用户
.....请问怎样实现,谢谢
user_id user_Fee
001 85.37
002 108.43
003 364.25
001 207.56
... ...现在想查询出所有100元分段为分组的用户数,如:
0-100元: 30个用户
100-200元: 20个用户
.....请问怎样实现,谢谢
from T
group by user_Fee/100
不能完全实现所描述的金额分段.
insert into @t select '001',85.37
union all select '002',108.43
union all select '003',364.25
union all select '001',207.56select 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 行)*/