日期:2014-05-16  浏览次数:20896 次

通话记录查询sql



这是一张记录用户A 给b 和 c 通话的记录
time是通话日期

如何从表中查出A所打的不同用户中的通话日期距离当前最近的记录 
上表 

期望结果:
A C 2012.5.22
A B 2012.3.23

------解决方案--------------------
SQL code
with t (c_id, p_id, time) as (
          select 'A', 'B', to_date('2012.03.12', 'yyyy.mm.dd') from dual
union all select 'A', 'B', to_date('2012.03.23', 'yyyy.mm.dd') from dual
union all select 'A', 'C', to_date('2012.04.12', 'yyyy.mm.dd') from dual
union all select 'A', 'C', to_date('2012.05.22', 'yyyy.mm.dd') from dual
)
select c_id, p_id, max(time) from t group by c_id, p_id;

------解决方案--------------------
SQL code

select * from 你的表 t1 where t1.time=(select max(time) from 你的表 t2 where t1.c_id=t2.c_id and t1.p_id=t2.p_id);