OkBublewrap

IBM db2 Product Analytics 본문

개발/SQL

IBM db2 Product Analytics

옥뽁뽁 2025. 3. 13. 02:11

IBM db2 Product Analytics

문제

1. 2023년 7월 ~ 9월 동안 직원들이 실행한 고유한 쿼리 수 기준 히스토그램 데이터 생성.

2. 이 기간 동안 아무런 쿼리도 실행하지 않은 직원 수도 계산해야 합니다.

 

풀이

select cnt as unique_queries, count(*) as employee_count
from (
  select e.employee_id, count(distinct q.query_id) as cnt
  from employees e
  left join queries q on e.employee_id = q.employee_id
  and q.query_starttime >= '2023-07-01'
  and q.query_starttime < '2023-10-01'
  group by e.employee_id
) sub1
group by unique_queries

1️⃣ sub1; left join: 2023년 3분기에 해당하는 테이블 병합

그 외의 기간은 데이터에 제외 (null 값으로 됨)

2️⃣ sub1; group by: employee_id 별 q.query 카운트

3️⃣ main; group by: q.query 카운트로 employee 다시 카운트

'개발 > SQL' 카테고리의 다른 글

Department vs. Company Salary  (0) 2025.03.15
User Shopping Sprees  (0) 2025.03.13
FAANG Stock Min-Max (Part 1)  (0) 2025.03.12
Second Day Confirmation  (0) 2025.03.12
Swapped Food Delivery  (0) 2025.03.10