• 카테고리

    질문 & 답변
  • 세부 분야

    데이터 분석

  • 해결 여부

    미해결

사용자별 월별 세션 접속 횟수의 구간별 분포 집계 SQL - where절

23.10.23 23:47 작성 23.10.23 23:47 수정 조회수 172

0

선생님 안녕하세요?

강의 잘 보고 있습니다.

월 말일 기준으로 2일전에 생성한 user를 제외하고, session 수를 카운트 하기 위해서 where 절 안에 아래와 같이 수업시간에 말씀주셨었는데요.

 

select a.user_id, date_trunc('month', visit_stime)::date as month, count(*) as monthly_user_cnt

from ga.ga_sess a

join ga.ga_users b on a.user_id = b.user_id

where b.create_time <= (date_trunc('month', b.create_time) +interval '1 month' - interval '1 day')::date -2

group by a.user_id, date_trunc('month', visit_stime)::date

 

이 부분에 의하면, 말일 기준으로 2일 전부터 말일까지 create된 user의 경우, 모든 month에서 session 데이터가 필터링 되는데 의도하신 바가 맞으신지요?

 

어떤 user가 9월 29일에 create 하고, 9월 30일에 session 기록이 있으면, 이건 count되지 않고,

10월 3일의 session 기록은 10월에 count하는 것이 의도하신 것이 아닌지요?

그럴경우에는 아래와 같이 where 절을 수정해야 count가 될 것 같아요.

where b.create_time <= (date_trunc('month', a.visit_stime)+interval '1 month' - interval '1 day')::date -2

 

답변 1

답변을 작성해보세요.

1

안녕하십니까,

말씀하신대로

where b.create_time <= (date_trunc('month', b.create_time) +interval '1 month' - interval '1 day')::date -2

로 filtering 하면 월말 구간에 생성된 모든 사용자들이 일괄 제외가 되는 군요. 제가 생각이 넘 짧았습니다.

권장해 주신 대로

where b.create_time <= (date_trunc('month', a.visit_stime)+interval '1 month' - interval '1 day')::date -2

로 filtering 하는 것이 더 목적에 맞는 쿼리이겠군요.

좋은 지적 감사합니다.