Search code examples
sqlwindow-functionsrankpartition

SQL - Partition restarted based on a column value


I need to create a new column that restarts at every 0 value of Column Repeated Call of each Customer_ID:

+-------------+---------+----------------------+---------------+
| Customer_ID | Call_ID | Days Since Last Call | Repeated Call |
+-------------+---------+----------------------+---------------+
|           1 |       1 | Null                 |             0 |
|           1 |       2 | 45                   |             0 |
|           1 |       3 | 0                    |             1 |
|           1 |       4 | 0                    |             1 |
|           1 |       5 | 0                    |             1 |
|           1 |       6 | 48                   |             0 |
|           1 |       7 | 1                    |             1 |
|           2 |       8 | Null                 |             0 |
|           2 |       9 | 1                    |             1 |
+-------------+---------+----------------------+---------------+

In to something like this:

+-------------+---------+----------------------+---------------+-------------+
| Customer_ID | Call_ID | Days Since Last Call | Repeated Call | Order_Group |
+-------------+---------+----------------------+---------------+-------------+
|           1 |       1 | Null                 |             0 |           1 |
|           1 |       2 | 45                   |             0 |           2 |
|           1 |       3 | 0                    |             1 |           2 |
|           1 |       4 | 0                    |             1 |           2 |
|           1 |       5 | 0                    |             1 |           2 |
|           1 |       6 | 48                   |             0 |           3 |
|           1 |       7 | 1                    |             1 |           3 |
|           2 |       8 | Null                 |             0 |           1 |
|           2 |       9 | 1                    |             1 |           1 |
+-------------+---------+----------------------+---------------+-------------+

Appreciate your suggestion, thanks!


Solution

  • You can use SUM() window function:

    select t.*,
      sum(case when Repeated_Call = 0 then 1 else 0 end) 
      over (partition by Customer_ID order by Call_Id) Order_Group 
    from tablename t
    

    See the demo (for MySql but it is standard SQL).
    Results:

    | Customer_ID | Call_ID | Days Since Last Call | Repeated_Call | Order_Group |
    | ----------- | ------- | -------------------- | ------------- | ----------- |
    | 1           | 1       |                      | 0             | 1           |
    | 1           | 2       | 45                   | 0             | 2           |
    | 1           | 3       | 0                    | 1             | 2           |
    | 1           | 4       | 0                    | 1             | 2           |
    | 1           | 5       | 0                    | 1             | 2           |
    | 1           | 6       | 48                   | 0             | 3           |
    | 1           | 7       | 1                    | 1             | 3           |
    | 2           | 8       |                      | 0             | 1           |
    | 2           | 9       | 1                    | 1             | 1           |