这种情况下SQL怎么写
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写,求指教!
------解决方案--------------------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
------解决方案--------------------掉了一个别名
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