Could I get some help creating this query please?
Users book product video slots to advertise ONE product video. The booking table captures these bookings. Booking spots are allocated to a user not to a product or video.
The #1-ranked video for the user’s #1-ranked product will be the one that is advertised.
Bookings table
id user_id position
---- ----- --------
1 100 1
2 150 2
Users table
Users can have 1-n bookings
id name
---- ----
100 John Smith
150 Herby Brown
Products table
A user can have 1-n products
id name user_id rank
--- ---- ------- ----
1 bike 100 1
2 stereo 100 2
3 computer 100 3
4 notebook 150 1
5 scooter 150 2
Videos table
A product can have 1-n videos
id name product_id user_id rank
--- ---- ----------- ------- ----
1 bike video1 1 100 1
2 bike video2 1 100 2
3 computer video 3 100 3
4 notebook video 4 150 1
5 scooter video 5 150 2
So, the query reads, in words:
For each booking record, get the #1 ranked video for the #1-ranked product for the booked user. Order the results by booking position.
some simplified query: