描述一下需求:就是查找用户访问最多的城市 
 
字段 imei , cityid , cnt (分别表示,用户ID,城市ID,访问次数)举例 1234   0001     33
     1234   0002     32
     1222   0001     20
     1222   0003     22显示结果1234 0001 33
1222 0003 22这句SQL怎么写啊 谢啦
Oracle 10G

解决方案 »

  1.   

    select  max(cnt) ,imei from tab_name
    group by imei;
      

  2.   

    没有规律 就是用户访问的城市 可以今天在北京,明天再上海,这个cityID就有一个 北京的ID 和上海的ID
      

  3.   

    select imei,cityid,cnt  from T1 a
    where cnt=(select max(cnt) from T1 b where a.imei=b.imei and a.cityid=b.cityid)
      

  4.   

    select  imei ,cityid ,max(cnt) ,from tab_name 
    group by imei,cityid ;
      

  5.   


    with tt as 
    (
    select '1234' imei, '0001' cityid, '33' cnt from dual
    union all 
    select '1234','0002','32' from dual
    union all 
    select '1222','0001','20' from dual
    union all 
    select '1222','0003','22' from dual
    )
    select imei,cityid,cnt from tt where  (imei,cnt) in (
    select imei,max(cnt)over(partition by imei) from tt );
    这是代码,tt是测试数据,换成表就可以了.select imei,cityid,cnt from tt where  (imei,cnt) in (
    select imei,max(cnt)over(partition by imei) from tt );
    ------------------------------------------------------------------------------
    Blog: http://blog.csdn.net/tianlesoftware
    网上资源: http://tianlesoftware.download.csdn.net
    相关视频:http://blog.csdn.net/tianlesoftware/archive/2009/11/27/4886500.aspx
    Q Q 群:62697716 
      

  6.   

    这会把每个访问城市的ID查询出来我在描述一下需求吧
    更具体一些imei cityid mnc cnt
    1234 0001   01  33
    1234 0001   02  34
    1234 0002   01  35
    1234 0002   02  33
    就只要显示 1234 0002  (cnt=35)这个结果
    你这个结果会是1234 0002 35 | 1234 0001 34 
      

  7.   

    select a.*
      from test a, (select id, max(time) time from test group by id) b
     where a.id = b.id
       and a.time = b.time
     或
     select *
       from test a
      where exists (select 1
               from (select id, max(time) time from test group by id) b
              where a.id = b.id
                and a.time = b.time)
      

  8.   

    id就是imei ,time就是cnt 
      

  9.   

    select imei , cityid , max(cnt) from table_name group by imei;