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?

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]

Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as