在select 语句中 用 max 时碰到的问题,请各位高人指点指点
我用下面语句 select max(ph) from vk_xmd where...... 时当ph 列是10内时 max(ph)取得值是对的,但当ph列的值有超过10时max(ph)取得值永远是 9 ph列类型是 varchar ,请各位指点指点如何解决或改正。在此先谢谢大家了。 问题点数:20、回复次数:7Top
1 楼wenzhong0751(雪渡)回复于 2005-01-31 11:48:12 得分 3
它们是按字符串比较的,2比10大,它们从左边取字母比,所以你的9要改为09才能比10小,或者用这样的Max(Cast(ph) as bigint)Top
2 楼chinaandys(降龙十八炒&&蛋炒饭)回复于 2005-01-31 11:49:38 得分 3
select max(case when ph<=10 then ph else 9 end) from tablename group by phTop
3 楼pbsql(风云)回复于 2005-01-31 11:50:04 得分 3
select cast(max(cast(ph as int)) as varchar) from vk_xmd where......Top
4 楼chinaandys(降龙十八炒&&蛋炒饭)回复于 2005-01-31 11:51:16 得分 3
sorry,这样
select ph=(case when ph<=10 then ph else 9 end) from test
如果取最大值 select ph=(case when ph<=10 then ph else 9 end) from test group by 分组列Top
5 楼chinaandys(降龙十八炒&&蛋炒饭)回复于 2005-01-31 11:52:29 得分 3
sorry,这样
select ph=(case when cast( ph as int)<=10 then ph else '9' end) from test
如果取最大值 select ph=(case when cast(ph as int)<=10 then ph else '9' end) from test group by 分组列
Top
6 楼lengxiaowei(小伟)回复于 2005-01-31 12:53:16 得分 3
select max(convert(int,ph)) from vk_xmd where......Top
7 楼Qihua_wu(小吴)回复于 2005-01-31 13:29:26 得分 2
select convert(varchar,max(convert(int,ph))) from yourtable where conditionTop




