sql - Finding first call for a case in last 7 days -


i have scenario there cases , multiple calls made customers against cases. these call logs in table has following columns -

id primary key int case_id int call_made_at timestamp 

i have find number of new calls(1st call made case) made in last 7 days , number of old calls (any call not 1st call case) in last 7 days.

i can use row_number() partition on case_id. lifetime of case id not much. doing partition on entire table seems bad. table become huge.

any suggestions?

i see aggregation, not window functions:

select sum(case when min_cma >= current_date - interval '7 day' 1 else 0 end) last_7_days,        sum(case when max_cma >= current_date - interval '7 day' ,                      min_cms < current_date - interval '7 day'                  1 else 0 end)  (select cl.case_id,              min(call_made_at) min_cma,              max(call_made_at) max_cma       call_logs cl       group cl.case_id      ) cl; 

you can add where max_cma >= current_date - interval '7 day' outer query. improve performance.


Comments