Querying to fill in numerical sequence

A

ArielZusya

I've got a table (tblMain) with the following (relevant) fields:

tblMain
.ID_Main, PrimaryKey
.FirstName, text of first name
.LastName, text of last name
.Seat, number holding the current seat number of this person

I'd like to query this table to figure out where the empty seats are and
then move the person currently seated in the numerically first seat greater
than a fixed number. So... let's say I have people seated in seats 1, 2, 3,
5, and 8, 9, 10, I'd like to move the person in seat 8 into seat 4... in
other words, I want to be able to always keep seats 1-5 filled as long as
there are people sitting in seats higher than 5. I can query for all those
in tblMain who have a seat:

SELECT tblMain.Seat
FROM tblMain
WHERE (((tblMain.Seat) Is Not Null));

and I can query for all those in tblMain with a seat greater than 5:

SELECT tblMain.Seat
FROM tblMain
WHERE (((tblMain.Seat) Is Not Null And (tblMain.Seat)>5));

I can also write an update query that will change the value of a person in a
given seat to the value of the empty seat... I'm just not sure how to 1)
query for the empty seat, 2) query for the lowest value in that second query,
and 3) loop through this until all the seats in the low range (1-5 in my
example) are filled as described above. Suggestions? Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top