devxlogo

Automatically Assigning Values

Automatically Assigning Values

Question:
[Joe Celko’s Hotel Rooms Puzzle]

Ron Hiner put this question on the Watcom forum on CompuServe. He had a data conversion project where he needed to automatically assign some values to use as part of the PRIMARY KEY to a table of hotel rooms.

The floor part of the PRIMARY KEY is the FOREIGN KEY to another table of floors within the building. The part of the hotel room key we need to create is the room number, which has to be a sequential number, starting at x01 for each floor x. The hotel is small enough that we know we will only have three digit numbers. The table is defined as follows:

    CREATE TABLE Hotel         (floor SMALLINT NOT NULL,        room SMALLINT NOT NULL,        PRIMARY KEY (floor, room),         FOREIGN KEY floor REFERENCES Bldg(floor);
Currently, the data in the table looks like this:
    floor  room    ===========        1  NULL         1  NULL         1  NULL         2  NULL         2  NULL         3  NULL  
Watcom (and other versions of SQL) has a NUMBER(*) function which begins at 1 and returns an incremented value for each row which calls it.

Is there an easy way via the NUMBER(*) function (or some other means) to automatically populate the room column? Mr. Hiner was thinking of some how using a GROUP BY floor number clause to restart the numbering back at 1.

The Watcom support people came up with this approach. First make one updating pass thru the whole database, to fill in the room numbers. This trick will not work unless you can guarantee that the Hotel table is updated in sorted order. As it happens, Watcom can guarantee just that with a clause on the UPDATE statement, thus:

    UPDATE Hotel         SET room =(floor*100)+NUMBER(*)        ORDER BY floor;
Which would give the results:
    floor room    ==========       1   101         1   102         1   103         2   204         2   205         3   306 
Followed by:
    UPDATE Hotel         SET room =(room – 3)         WHERE floor =2;    UPDATE Hotel         SET room =(room – 5)         WHERE floor =3;
Which would give the correct results:
    floor room    ==========       1   101         1   102         1   103         2   201         2   202         3   301
Can you top this ? without using the ORDER BY clause?

See also  Why ChatGPT Is So Important Today

Answer:
I would use SQL to write SQL statements. This is a neat trick which is not used enough. Just watch your quotation marks when you do it and remember to convert numerics to characters, thus:

    SELECT DISTINCT        ‘UPDATE Hotel SET room =(‘        || CAST (floor AS CHAR(1))        || ‘* 100)+NUMBER(*) WHERE floor =’        || CAST (floor AS CHAR(1))        || ‘;’
This statement will write a result table with one column that has test like this:
    UPDATE Hotel SET room =(1*100)+NUMBER(*) WHERE floor =1;    UPDATE Hotel SET room =(2*100)+NUMBER(*) WHERE floor =2;    UPDATE Hotel SET room =(3*100)+NUMBER(*) WHERE floor =3;    …
This does not depend on the order of the rows in the table.

Puzzle provided courtesy of:
Joe Celko
[email protected]

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist