当前位置:主页>销售管理软件> 列表

请教MAX()函数的使用技巧

仓库管理软件版1楼: 本人有一表(aa1)字段如下:
字段 exch_id, msisdn ,fee
对应值 广东 134******** 1345.89
上海 137******** 56.09
其中exch_id为归属省份,msisdn为电话号码,fee为金额
现要[gold]求出每个省份销费金额排在第一位的电话号码[/gold]


我用了下列语句
select distinct exch_id,msisdn,max(fee) from aa1 group by exch_id
得出结果是:1省份已唯一----------正确
2fee已是最大值-------正确
3msisdn不是原表aa1对应的最大值fee的电话号码------错误
[gold]请教各位给出合理的求法(sql语句)[/gold]

2楼: select exch_id,msisdn,max(fee) from aa1 group by exch_id 如管家进销存

3楼: select exch_id,max(fee) from aa1 group by exch_id

4楼: select aa1.* from(select exch_id,max(fee) as fee from aa1 group by exch_id)b
inner join aa1 on aa1.exch_id=b.exch_id and aa1.fee=b.fee

5楼: 勇者说的很对,
今天上午我也是要求一个表内每个班级的总分第一名的集合,用的就是这种方法,完全解决 !


楼主可以揭帖,散分!

6楼: HuangJH说的很正确
参考代码:
select aa1.exch_id,aa1.msisdn,aa1.fee
from aa1,(select exch_id,max(fee)as fee from aa1 group by exch_id)as A
where aa1.exch_id=A.exch_id and aa1.fee = A.fee