时间序列下销售代表(rep)状态计算及SQL实现问询
First, let's clarify the core requirement: we need to track each rep's status as they acquire new clients over time, not just their final status based on total clients. That means using a running (cumulative) client count per rep, ordered by the client registration date, to reflect the time-series progression.
Key Fix to Your Partial Query
Your initial code uses a total count per rep, but we need to adjust it to calculate a cumulative count instead. Here's the complete, refined SQL:
Complete SQL Implementation
SELECT rep_signup_date, rep_id, client_registration_date, client_id, -- Calculate cumulative client count for the rep up to each registration date COUNT(client_id) OVER ( PARTITION BY rep_id ORDER BY client_registration_date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) AS cumulative_client_count, -- Map cumulative count to status per the rules CASE WHEN COUNT(client_id) OVER ( PARTITION BY rep_id ORDER BY client_registration_date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) = 1 THEN '1' WHEN COUNT(client_id) OVER ( PARTITION BY rep_id ORDER BY client_registration_date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) = 2 THEN '2' WHEN COUNT(client_id) OVER ( PARTITION BY rep_id ORDER BY client_registration_date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) >= 3 THEN '3' END AS status FROM your_table_name; -- Replace with your actual table name
Breakdown of Critical Parts
- Cumulative Count Logic: The window function
PARTITION BY rep_id ORDER BY client_registration_dategroups rows by rep and orders them by when each client was registered. TheROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROWensures we count all clients the rep has acquired up to and including the current row's date. - Status Mapping: The CASE statement directly applies your rules to the cumulative count:
- 1 client → Status 1
- 2 clients → Status 2
- 3+ clients → Status 3
Sample Output Using Your Data
When run against your sample dataset, the result will look like this:
| rep_signup_date | rep_id | client_registration_date | client_id | cumulative_client_count | status |
|---|---|---|---|---|---|
| 1/2/2018 | 1 | 1/5/2018 | 1 | 1 | 1 |
| 1/2/2018 | 1 | 1/9/2018 | 2 | 2 | 2 |
| 1/2/2018 | 1 | 2/15/2018 | 3 | 3 | 3 |
| 1/4/2018 | 2 | 2/3/2018 | 4 | 1 | 1 |
| 1/4/2018 | 2 | 3/9/2018 | 5 | 2 | 2 |
| 2/1/2018 | 3 | 2/2/2018 | 6 | 1 | 1 |
Optional Cleanup with CTE
To avoid repeating the window function (and make the query easier to maintain), use a CTE to calculate the cumulative count once:
WITH rep_client_timeline AS ( SELECT rep_signup_date, rep_id, client_registration_date, client_id, COUNT(client_id) OVER ( PARTITION BY rep_id ORDER BY client_registration_date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) AS cumulative_client_count FROM your_table_name ) SELECT *, CASE WHEN cumulative_client_count = 1 THEN '1' WHEN cumulative_client_count = 2 THEN '2' WHEN cumulative_client_count >=3 THEN '3' END AS status FROM rep_client_timeline;
内容的提问来源于stack exchange,提问作者user8834780

