Automatically Assigning Values

[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?

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]

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS