phone表:
key  mobile   create_time
a     123      2012-12-10
a     456      2012-12-11
b     abc      2012-11-12
b     def      2012-11-13
b     ghi      2012-11-14查询key,key的数量,key最近的create_time,key最近create_time的mobile预计结果如下:
key   count(key) 最近的mobile  最近的create_time
a      2           456          2012-12-11
b      3           ghi          2012-11-14想来半天,不知道SQL写,求指教!

解决方案 »

  1.   

    select a.key key, mobile mobile, c.createtime cteateTime, b.num num
              from tableName a
              left join (select key, count(*) num from tableName group by key) b on a.key =
                                                                                   b.key
              right join (select key,max(createtime) createtime from tableName group by key) c on c.key=a.key and a.createtime=c.createtime
      

  2.   

    select a.key , count_key,mobile,a.create_time from 
    (
     select key , count(key) , max(create_time) create_time from phone group by key
    ) a , phone b where a.create_time = b.create_time测试通过
      

  3.   

    掉了一个别名select a.key , count_key,mobile,a.create_time from (  select key , count(key) count_key  , max(create_time) create_time from phone group by key) a , phone b where a.create_time = b.create_time  and a.key = b.key
      

  4.   

    加入createtime一样的怎么办?这样就造成key相同的记录了!
      

  5.   

    假如createtime一样的怎么办?这样就造成key相同的记录了!请问怎么解决?