I have got function named cdr_get_call_log. I m getting my data from this function. As you see in example data record number 3 and 4 is got same source_caller_id and same day but different time. So i want if there are same source_caller_id in my data i want to get only record that start_time is latest
select source_caller_id,destination_dn,recording_url,start_time
from (
select *
from cdr_get_call_log
where destination_type=0
and talking_duration is not null
and source_caller_id not like 'Ext%'
and source_caller_id like '0%'
and destination_dn in('628','627','629','630','631','626','600','632')
and source_caller_id<> '09903114243'
and subrow_desc_number='1'
and trim(recording_url)<> ''
) as a
Table Structure
| source_caller_id | destination_dn | recording_url | start_time |
|------------------|----------------|--------------------|-----------------------|
| 1.05356785544 | 627 | [627]/20180230.wav | 01/12/2018 11.12:39 |
| 2.05551165244 | 632 | [632]/20180230.wav | 04/12/2018 09.12:39 |
| 3.05556665544 | 621 | [627]/20180230.wav | 06/12/2018 05.12:39 |
| 4.05556665544 | 626 | [627]/20180230.wav | 06/12/2018 05.15:39 |
答案 0 :(得分:1)
You can use distinct on
:
with q as (<your query here>)
select distinct on (caller_id, date_trunc('day', start_time)) q.*
from q
order by caller_id, date_trunc('day', start_time), start_time desc;