Skip to content
Advertisement

Update Customer table from selected Ids and assign one of three distinct values to each

I have a fairly complex query which results in a list of Customer Ids where they have not ordered within a defined period, have no Rep assigned to them, and are business customers rather than private. This simply returns a sequential list of Ids which match the BIGINT primary keys of the matching customers, like this:

I now want to update the records with those Ids and assign to each a RepId from one of three Sales Reps:

My problem is to assign the rep Ids. Actually doing it is easy, as per this pseudocode:

The problem is calculating the relevant Id. I have considered a number of approaches including selecting RowNumber() and using the number and its relation to three to select the number, and incrementing a variable and using that to assign the relevant Id, but I cannot get it to work. I also considered splitting the results into three and assigning separately but again cannot work out how to do it and in any case that is definitely not an ideal approach – for a start with imported ordered data that would skew the results giving each rep customers in a range depending on entry order.

The desired result would be something like this:

The order of the RepIds is not necessarily critical, but shown as I originally envisaged it the idea being to share the customers between the Reps as evenly as possible.

EDIT – the final solution

I was able to apply the list of Ids by simply adding a WHERE clause to the cte_customers proposed by @forpas:

That worked perfectly!!

Advertisement

Answer

Assuming there is a table Reps with the RepIds that you want to use to update the table Customers, you can use ROW_NUMBER() window function to join the tables in the UPDATE statement:

See the demo.

If you want to supply only specific RepIds use an additional CTE:

See the demo.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement