은행 거래 내역을 보고 지출 패턴을 이해하려는 개인적인 필요성이든, 영업 활동을 보고 조직의 영업 성과를 개선하려는 것이든, 진정한 인사이트는 거래 수준 데이터를 추가로 집계하고 요약해야만 볼 수 있습니다. 기존 RDBMS는 SQL 집계의 표현력을 통해 이러한 기능을 제공합니다. 창 함수는 ANSI SQL 표준에 추가되어 SQL 구조로 더 복잡한 질문을 표현할 수 있는 기능을 더욱 확장했습니다.
이제 6.5 릴리스에서 Couchbase N1QL은 창 함수 및 공통 테이블 표현식(CTE)을 지원합니다. 개발자는 더 복잡한 비즈니스 사용 사례를 충족하도록 애플리케이션을 확장할 수 있을 뿐만 아니라 데이터 분석가가 Excel에서 추가 후처리를 수행하지 않고도 필요한 답을 얻을 수 있습니다.
이 글에서는 매우 일반적인 비즈니스 질문 두 가지를 해결하기 위해 N1QL 창 기능과 CTE를 활용하는 방법에 대한 몇 가지 예시를 집중적으로 살펴보겠습니다.
고객별 영업 활동 기간
영업 활동 관리 시스템의 첫 번째 예에서는 영업팀이 2019년 1월 동안 고객과 작업한 시간을 보여주는 보고서를 제공하고자 합니다. 쿼리를 두 단계로 나누겠습니다:
1) 영업팀이 고객과 진행한 미팅 목록과 미팅 시간을 가져옵니다. 모든 고객의 총 소요 시간 'total_time_spent'는 전체 결과 집합에 대해 합산을 수행하는 빈 OVER () 절을 사용하여 약속 시간을 합산하여 계산합니다. 고객별 총 소요 시간 'account_time_spent'는 동일한 구문을 사용하지만 PARTITION 절에 'accid'를 사용합니다.
|
1 2 3 4 5 6 7 8 |
SELECT c.name, a.startDate, a.title, a.duration, SUM(a.duration) OVER() as total_time_spent, SUM(a.duration) OVER(PARTITION BY a.accid) AS account_time_spent FROM crm a INNER JOIN crm c ON a.accid = c.id AND c.type='account' WHERE a.type='activity' AND a.activityType='Appointment' AND a.startDate BETWEEN '2018-01-01' and '2018-01-31' |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
account_time_spent duration name startDate title total_time_spent 30 "30" "Smith, Avila and Cisneros" "2018-01-24 15:00:00" "Switchable coherent adapter" 25770 510 "150" "Riddle Ltd" "2018-01-05 15:30:00" "Streamlined intermediate intranet" 25770 510 "90" "Riddle Ltd" "2018-01-28 14:30:00" "Advanced solution-oriented synergy" 25770 510 "90" "Riddle Ltd" "2018-01-01 15:00:00" "Ameliorated object-oriented methodology" 25770 510 "30" "Riddle Ltd" "2018-01-10 08:00:00" "Object-based multi-state firmware" 25770 510 "150" "Riddle Ltd" "2018-01-23 09:00:00" "Multi-layered systematic software" 25770 120 "30" "Foster Inc" "2018-01-29 09:30:00" "Public-key bottom-line database" 25770 120 "30" "Foster Inc" "2018-01-09 13:00:00" "Quality-focused local emulation" 25770 120 "60" "Foster Inc" "2018-01-02 08:00:00" "Digitized motivating matrix" 25770 120 "30" "Williams Ltd" "2018-01-22 08:30:00" "Versatile heuristic workforce" 25770 120 "30" "Williams Ltd" "2018-01-24 13:30:00" "Front-line 4thgeneration help-desk" 25770 120 "60" "Williams Ltd" "2018-01-14 14:30:00" "Visionary upward-trending success" 25770 330 "150" "Reid Ltd" "2018-01-21 10:30:00" "Profound logistical archive" 25770 330 "30" "Reid Ltd" "2018-01-13 13:30:00" "Down-sized coherent access" 25770 330 "120" "Reid Ltd" "2018-01-02 12:00:00" "Front-line object-oriented moderator" 25770 330 "30" "Reid Ltd" "2018-01-12 09:30:00" "Programmable reciprocal infrastructure" 25770 .... |
2) 그런 다음 두 지표를 사용하여 팀이 각 고객과 보낸 전체 시간에서 차지하는 비율을 도출합니다.
|
1 2 3 4 5 6 7 8 9 10 |
SELECT c.name, ROUND(( SUM(SUM(a.duration)) OVER(PARTITION BY a.accid) / SUM(SUM(a.duration)) OVER()),2) as pct_time FROM crm a INNER JOIN crm c ON a.accid = c.id AND c.type='account' WHERE a.type='activity' AND a.activityType='Appointment' AND a.startDate BETWEEN '2018-01-01' and '2018-01-31' GROUP BY c.name, a.accid ORDER BY 2 DESC |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
name pct_time "Johnson, Adams and Kelly" 0.17 "Davis Group" 0.08 "Gilbert-Morris" 0.08 "Torres and Sons" 0.07 "Reid Ltd" 0.07 "Medina-Daniels" 0.07 "Riddle Ltd" 0.05 "Henderson and Sons" 0.05 "Gill and Sons" 0.05 "Garcia-Young" 0.05 "Sullivan PLC" 0.03 "Brown-Rogers" 0.03 "Foster Inc" 0.03 "Wheeler Inc" 0.03 "Jarvis-Small" 0.03 "Jones-Fox" 0.03 "Lloyd, Blair and Pruitt" 0.03 "Vaughn LLC" 0.02 |
월별 영업 활동
이 두 번째 예에서는 2018년 한 해 동안 영업 관련 작업 수가 월별로 어떻게 변화했는지 쿼리로 보여줍니다. 이 쿼리는 쿼리의 가독성을 높이기 위해 N1QL CTE 기능을 활용하며, 이전 기간의 작업 수를 얻기 위해 LAG 창 기능도 활용합니다.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
WITH current_period_task AS ( SELECT DATE_TRUNC_STR(a.startDate,'month') AS month, COUNT(1) AS current_period_task_count FROM crm a WHERE a.type='activity' AND a.activityType = 'Task' AND DATE_PART_STR(a.startDate,'year') = 2018 GROUP BY DATE_TRUNC_STR(a.startDate,'month') ), last_period_task AS ( SELECT x.month, x.current_period_task_count, LAG(x.current_period_task_count) OVER ( ORDER BY x.month) AS last_period_task_count FROM current_period_task x ) SELECT b.month, b.current_period_task_count, ROUND(((b.current_period_task_count - b.last_period_task_count ) / b.last_period_task_count),2) AS MoMChg FROM last_period_task AS b |
- 첫 번째 CTE인 'current_period_task'는 달력 월별로 Task 그룹 유형의 모든 활동에 대한 개수를 검색하는 쿼리를 정의합니다.
- 두 번째 CTE인 'last_period_task'는 첫 번째 CTE에서 읽으며, 창 함수 LAG를 활용하여 이전 달의 task_count를 반환합니다. LAG 함수가 작동하려면 ORDER BY 절이 중요합니다.
- 메인 쿼리는 두 번째 CTE인 'last_period_task'를 읽고 월별 계산을 도출합니다.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
MoMChg current_period_task_count month 283 "2018-01-01" -0.08 260 "2018-02-01" 0.1 287 "2018-03-01" -0.08 264 "2018-04-01" 0.11 292 "2018-05-01" 0 293 "2018-06-01" -0.03 285 "2018-07-01" 0 285 "2018-08-01" 0 284 "2018-09-01" 0 283 "2018-10-01" -0.05 268 "2018-11-01" 0.06 285 "2018-12-01" |
리소스
6.5의 기능이 마음에 드셨는지, 앞으로 비즈니스에 어떤 도움이 될지 여러분의 의견을 듣고 싶습니다. 댓글을 통해 의견을 공유해 주시거나 포럼.