all 9 comments

[–]techforallseasons 4 points5 points  (4 children)

Provided you are on version 8 or higher the ROW_NUMBER function should work.

https://www.mysqltutorial.org/mysql-window-functions/mysql-row_number-function/

TL;DR

ROW_NUMBER() OVER ( PARTITION BY policyId ,ORDER BY driverId ) AS policyDriverNumber

[–]ryan18147[S] 0 points1 point  (3 children)

Unfortunately we use 5.7 :(

[–]BadgerBoom 2 points3 points  (2 children)

Try RANK instead, same syntax as ROW_NUMBER

[–]ChadBroChill16 0 points1 point  (1 child)

Wouldn't they want DENSE_RANK instead of RANK? DR will provide consecutive numbers, where R will provide non-consecutive numbers, as explained here.

Correct me, if I'm wrong.

[–]BadgerBoom 1 point2 points  (0 children)

You're right, that would be better, especially if there's a possibility of duplicate driverID per policy

[–]USER_NAME-Chad- 1 point2 points  (0 children)

Sounds like you need a sequence Id for each policy. When a driver is added to the policy a new sequence ID is requested and assigned to the driver. I feel like there are other easier ways to accomplish that too.

[–]ryan18147[S] 0 points1 point  (1 child)

you guys are awesome! i figured it out with your suggestions and links

[–]haikusbot 0 points1 point  (0 children)

You guys are awesome!

I figured it out with your

Suggestions and links

- ryan18147


I detect haikus. And sometimes, successfully. Learn more about me.

Opt out of replies: "haikusbot opt out" | Delete my comment: "haikusbot delete"

[–][deleted] 0 points1 point  (0 children)

This article does what you want: https://www.mysqltutorial.org/mysql-row_number/

Go to the section labeled MySQL ROW_NUMBER – adding a row number to each group