Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Language: SQL Server
Expertise: Beginner
Mar 24, 1997



Application Security Testing: An Integral Part of DevOps

Finding Exactly One Non-Zero Column

[Joe Celko's One In Ten Puzzle]

Alan Flancman ran into a problem with some legacy system data that had been moved over to a SQL database. The table looked like this:

    CREATE TABLE MyTable  
        (keycol INTEGER NOT NULL,
        f1 INTEGER NOT NULL, 
        f2 INTEGER NOT NULL, 
        f10 INTEGER NOT NULL, 
The columns f1 through f10 were an attempt to flatten out an array into a table. What he wanted was an elegant way to test against the f1 through f10 columns to find the rows that had exactly one non-zero in their f-columns.

How many different approaches can you find?

Solution One:

You could use the Sign() function in Sybase and other SQL products. This function returns -1, 0, or +1 if the argument is negative, zero, or positive respectively. Assuming that your numbers are zero or greater, you simply write:

    SELECT * 
        FROM MyTable
        WHERE Sign(f1) + Sign(f2) + ... + Sign(f10) = 1;
to find a single non-zero value. If you can have negative values, then make the functions Sign(ABS(fn)).

The Sign() function can be written with the CASE expression in SQL-92 as:

    CASE WHEN x > 0 THEN 1
        WHEN x = 0 THEN 0
        WHEN x < 0 THEN -1

Solution Two:

Since the fields are really an attempt to fake an array, you should normalize this table into 1NF, like so:

    CREATE TABLE Foobar  
        (keycol INTEGER NOT NULL,
        f INTEGER NOT NULL, 
        PRIMARY KEY (keycol, i));
The extra column i is really the subscript for the array. You now view the problem as finding an entity which has exactly nine zero valued fs, instead of as finding an entity which has exactly one non-zero valued f. That is suddenly easy:
    SELECT keycol
        FROM Foobar
        WHERE f = 0
        GROUP BY keycol  
        HAVING COUNT(*) = 9;
You can create a VIEW that has the structure of Foobar, but things are going to run pretty slow unless you have a good optimizer:
    CREATE VIEW Foobar (keycol, f)  
        AS SELECT keycol, f1 FROM MyTable
        SELECT keycol, f2 FROM MyTable
        SELECT keycol, f10 FROM MyTable;

Solution Three:

This depends on a feature of SQL-92 that is not generally available yet.

    SELECT * 
        FROM MyTable
        WHERE (f1, f2, ... , f10) IN
            ((f1, 0, 0, 0, 0, 0, 0, 0, 0, 0),
            (0, f2, 0, 0, 0, 0, 0, 0, 0, 0),
            (0, 0, 0, 0, 0, 0, 0, 0, 0, f10));
In SQL-92, you can use row constructors in comparison predicates. The IN predicate expands into a sequence of OR-ed equality predicates. The row-wise version of equality is then done on a position by position basis, where all corresponding values must be equal.

Puzzle provided courtesy of:
Joe Celko

DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date