Вопрос

I have data of calls for customers. I want to get those customers between two dates that have activity against every date. They did at least one activity every day. I tried following query.

Following is the query:

select date_id , count (distinct customer_id) from usage_analysis 
where usage_direction_type_id = 1
and date_id => 20130608 and date_id <= 20130612
group by date_id 

That returns:

DATE_ID         COUNT
----------------------------
20130608        23451
20130609        9878
20130610        56122   
20130611        7811
20130612        12334

But I want to get those customers that are common in each group. It may happen a person who called on 8 June does not exist on the next day. So I only want those customers that exist in every group.

Any idea who can I do that in SQL?

Это было полезно?

Решение

You can count the distinct dates for each customer. Only customers with five distinct dates would then pass the test. The following provides the list of customers:

select customer_id
from usage_analysis 
where usage_direction_type_id = 1 and
      date_id >= 20130608 and date_id <= 20130612
group by customer_id
having count(distinct date_id) = 5

Другие советы

@Gordon Linoff answer should be working fine for your situation. When you tried with 2 days, did you make sure to change the count value from 5 to 2?

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top