| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1106 人关注过本帖
标题:sql来查不同数字段的人数
只看楼主 加入收藏
lbaichun
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2007-4-24
收藏
 问题点数:0 回复次数:2 
sql来查不同数字段的人数
我的表是这种的
ID    段数
1     2.0
2     3.0
3     2.5
4     2.7
5     4.0
6     3.5
我想用一句sql来实现这种效果
2.0-3.0 有4个id
3.0-4.0 有3个id
搜索更多相关主题的帖子: sql 数字 
2008-01-16 10:41
xiaoyutou
Rank: 1
等 级:新手上路
帖 子:29
专家分:0
注 册:2007-3-29
收藏
得分:0 
这样可以吗?
select   case
                when (段数>=2.0 and 段数<=3.0) then '2.0-3.0'
                when (段数>3.0 and 段数<=4.0)   then'3.0-4.0'
                 end
           
       count( case
       when (段数>=2.0 and 段数<=3.0) then 1
       when (段数>3.0 and 段数<=4.0)   then 1
       end
) from 表名
  group by     ( case
                when (段数>=2.0 and 段数<=3.0) then '2.0-3.0'
                when (段数>3.0 and 段数<=4.0)   then'3.0-4.0'
                end
               )

凡事由其自然,遇了处之泰然,得意之时淡然,失意之时坦然,艰辛曲折必然,历尽沧桑悟然。
2008-01-16 13:27
purana
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:广东-广州
等 级:版主
威 望:66
帖 子:6039
专家分:0
注 册:2005-6-17
收藏
得分:0 
界限最好做一个表出来.

create table tb(id int,段数 decimal(5,1))
insert into tb select 1,     2.0
insert into tb select 2 ,    3.0
insert into tb select 3  ,   2.5
insert into tb select 4   ,  2.7
insert into tb select 5    , 4.0
insert into tb select 6     ,3.5

select rtrim(b.上限)+'~'+rtrim(b.下限) 段数,count(*) 数量
from tb a
  inner join
   (select 上限=2.0,下限=3.0 union all select 3.0,4.0) b
on a.段数>=b.上限 and a.段数<=b.下限
group by b.上限,b.下限

drop table tb

/*
段数            数量         
-------------- -----------
2.0~3.0           4
3.0~4.0           3

(所影响的行数为 2 行)
*/

我的msn: myfend@
2008-01-16 13:35
快速回复:sql来查不同数字段的人数
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013349 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved