Blog Post

Stupid T-SQL Tricks – Part 1: Logarithms

,

Not just for math geeks, logarithms and their inverse functions (the exponentials) can have useful and often unexpected capabilities in T-SQL.

    “Heeeeeere’s Johnny!” – Ed McMahon introducing Johnny Carson for the Tonight Show

If you’re old enough to remember the Tonight Show hosted by Johnny Carson, how can you not remember the iconic Stupid Pet Tricks skits, and the later tributes to those done by David Letterman?

While Carson’s animal antics were meant to entertain, my Stupid T-SQL Tricks are meant to be thought provoking and also add a tool or two to your T-SQL tools chest in the event that the need arises. So maybe they aren’t so stupid after all (and besides it’s my blog and I like the title). This will be the first of a series of blogs offering two to four specialty tips that are all somehow related. Today’s tricks will all utilize logarithms to do their magic.

Calculate a Factorial

Statisticians will recognize the factorial, a number that is represented as N!, where n=0, 1, 2, 3, … and for example 4! = 1*2*3*4 = 24. In the somewhat distant past I had the chutzpah to suggest that it could be done with a recursive Common Table Expression (CTE). Although in my article Exploring Recursive CTEs by Example, I wasn’t really advocating that approach. It was more like I was trying to help folks better understand how recursive CTEs work. Nowadays, I would never do it that way because there is something better.

Let’s start with a Tally table that generates numbers from 1 to 10. In our query below, we’ll SUM those numbers.

WITH Tally(n) AS
(
    SELECT TOP 10 ROW_NUMBER() OVER (ORDER BY (SELECT NULL))
    FROM (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) a(b)
    CROSS JOIN(VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) b(c)
)
SELECT SUM(n)
FROM Tally;
-- Results:
55

Too bad there isn’t a PROD() built-in function in T-SQL. Or is there? Consider logarithms and how they can convert a SUM() into a PROD() using a few simple calls to the right built-in functions in the proper sequence. Let’s try these two queries:

WITH Tally(n) AS
(
    SELECT TOP 10 ROW_NUMBER() OVER (ORDER BY (SELECT NULL))
    FROM (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) a(b)
    CROSS JOIN(VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) b(c)
)
SELECT EXP(SUM(LOG(n)))
FROM Tally;
SELECT 1*2*3*4*5*6*7*8*9*10;
-- Results:
3628800.00000001
3628800

Those two results are remarkably close, the first one of course being slightly imprecise due to the fact that LOG and EXP are both returning a FLOAT data type. Since we know a factorial number must be an integer, we can convert it as follows, so now it returns the exact integer value of interest (3628800).

WITH Tally(n) AS
(
    SELECT TOP 10 ROW_NUMBER() OVER (ORDER BY (SELECT NULL))
    FROM (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) a(b)
    CROSS JOIN(VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) b(c)
)
SELECT CAST(ROUND(EXP(SUM(LOG(n))), 0) AS BIGINT)
FROM Tally;

While it unlikely that you’ll often have the need to calculate factorials in T-SQL, you may occasionally have the need to calculate a product across multiple rows for a column. So this technique will work for that also.

The Running Product

Using SQL 2012, we can use this same technique to calculate a running product (similar to a running total) across multiple rows in a table. To do this, we’ll use a window frame.

WITH Tally(n) AS
(
    SELECT TOP 10 ROW_NUMBER() OVER (ORDER BY (SELECT NULL))
    FROM (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) a(b)
    CROSS JOIN(VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) b(c)
)
SELECT n
    ,CAST(ROUND(EXP(
        SUM(LOG(n)) OVER
            (
                ORDER BY n ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
            )
        ), 0) AS BIGINT)
FROM Tally;
-- Results:
1      1
2      2
3      6
4      24
5      120
6      720
7      5040
8      40320
9      362880
10     3628800

Be careful though! These numbers are going to grow really quickly, so hopefully you won’t be doing this over many, many rows.

Coded, Numeric Fields with and without Leading Zeroes

In many applications you’ll find columns that are coded as numbers, but are stored as characters. This is, of course, a best practice when you don’t expect to be doing arithmetic with the contents. Let’s use a bit of a contrived example in the guise of a Sales Order Details table:

CREATE TABLE #SO_Details
(
    SO_Number           VARCHAR(11)
    ,SO_Detail          VARCHAR(4)
    ,Quantity           INT
    ,Price              MONEY
    ,Extended_Price     AS (Quantity*Price)
);
INSERT INTO #SO_Details
    (
        SO_Number, SO_Detail, Quantity, Price
    )
VALUES ('2013SO00001', '1', 3, 15.20),('2013SO00001', '2', 3, 9.40)
    ,('2013SO00001', '3', 1, 11.50),('2013SO00001', '4', 2, 11.55)
    ,('2013SO00001', '5', 2, 14.30),('2013SO00001', '6', 10, 13.32)
    ,('2013SO00001', '7', 5, 19.42),('2013SO00001', '8', 6, 10.15)
    ,('2013SO00001', '9', 4, 12.15);
SELECT *
FROM #SO_Details
ORDER BY SO_Number, SO_Detail;
-- Results:
SO_Number     SO_Detail Quantity   Price   Extended_Price
2013SO00001   1         3          15.20   45.60
2013SO00001   2         3          9.40    28.20
2013SO00001   3         1          11.50   11.50
2013SO00001   4         2          11.55   23.10
2013SO00001   5         2          14.30   28.60
2013SO00001   6         10         13.32   133.20
2013SO00001   7         5          19.42   97.10
2013SO00001   8         6          10.15   60.90
2013SO00001   9         4          12.15   48.60

We’ve used a computed column to calculate Extended_Price as Quantity * Price. Let’s suppose that we now need to insert an additional SO_Detail row.

BEGIN TRANSACTION T1;
INSERT INTO #SO_Details
    (
        SO_Number, SO_Detail, Quantity, Price
    )
VALUES ('2013SO00001', '10', 2, 18.88);
SELECT *
FROM #SO_Details
ORDER BY SO_Number, SO_Detail;
ROLLBACK TRANSACTION T1;
-- Results:
SO_Number     SO_Detail Quantity   Price   Extended_Price
2013SO00001   1         3          15.20   45.60
2013SO00001   10        2          18.88   37.76
2013SO00001   2         3          9.40    28.20
2013SO00001   3         1          11.50   11.50
2013SO00001   4         2          11.55   23.10
2013SO00001   5         2          14.30   28.60
2013SO00001   6         10         13.32   133.20
2013SO00001   7         5          19.42   97.10
2013SO00001   8         6          10.15   60.90
2013SO00001   9         4          12.15   48.60

Oh my! Those weren’t the results we wanted because our new record (SO_Detail=10) is sorted into the wrong spot! Of course, this can easily be addressed by changing the ORDER BY so that it CASTs the SO_Detail column to INT, but then that’s why I said this example is a bit contrived. Note that we rolled back the transaction so that our new record 10 was not really saved in the table.

We could use our newfound knowledge of logarithms to address this. In our prior example, we used a natural log, but in this example we’ll use a base 10 logarithm. Let’s take a look at a property of base 10 logarithms that we can use to add a leading zero to each of the entries from 1 to 9. To show this property, we’ll fall back to our Tally table.

WITH Tally(n) AS
(
    SELECT TOP 100 ROW_NUMBER() OVER (ORDER BY (SELECT NULL))
    FROM (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) a(b)
    CROSS JOIN(VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) b(c)
)
SELECT n, LOG10(n), FLOOR(LOG10(n))
    ,POWER(10, 1+FLOOR(LOG10(n)))
    ,MAX(n) OVER (PARTITION BY NULL)
FROM Tally
WHERE n IN(1, 9, 10, 11, 99, 100);
-- Results:
1     0                   0    10     100
9     0.954242509439325   0    10     100
10    1                   1    100    100
11    1.04139268515823    1    100    100
99    1.99563519459755    1    100    100
100   2                   2    1000   100

Let’s say that we know that our range of SO_Detail is 1 through 9. In that case, we can use a formula like the following to prepend exactly the right number of leading zeroes (each is shown for the number 9).

SELECT RIGHT(10+9, 1), RIGHT(100+9, 2), RIGHT(1000+9, 3);
-- Results:
9     09   009

The numbers 10, 100 and 1000, as well as the 1, 2 and 3 can be derived directly from the results returned by the former query. Let’s put this all together.

WITH Tally(n) AS
(
    SELECT TOP 100 ROW_NUMBER() OVER (ORDER BY (SELECT NULL))
    FROM (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) a(b)
    CROSS JOIN(VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) b(c)
)
SELECT n, RIGHT(POWER(10, 1+FLOOR(LOG10(m))) + n, 1+FLOOR(LOG10(m)))
FROM
(
    SELECT n, m=MAX(n) OVER (PARTITION BY NULL)
    FROM Tally
) a
WHERE n IN (1, 9, 10, 11, 99, 100);
-- Results:
1      001
9      009
10     010
11     011
99     099
100    100

You see, our new formula using RIGHT not only gets us character strings. It also prepends exactly the correct number of zeroes so that the new result is sortable directly on the resulting character string.

Let’s return to our SO Details table and see how we can apply this new formula to normalize our detail line numbers.

BEGIN TRANSACTION T1;
INSERT INTO #SO_Details
    (
        SO_Number, SO_Detail, Quantity, Price
    )
VALUES ('2013SO00001', '10', 2, 18.88);
WITH SO_Details AS
(
    SELECT SO_Number, SO_Detail, Quantity, Price
        ,m=MAX(CAST(SO_Detail AS INT)) OVER (PARTITION BY NULL)
    FROM #SO_Details
    WHERE SO_Number = '2013SO00001'
)
UPDATE SO_Details
SET SO_Detail = RIGHT(POWER(10, 1+FLOOR(LOG10(m))) + SO_Detail, 1+FLOOR(LOG10(m)));
SELECT *
FROM #SO_Details
ORDER BY SO_Number, SO_Detail;
ROLLBACK TRANSACTION T1;
-- Results:
SO_Number     SO_Detail  Quantity   Price   Extended_Price
2013SO00001   01         3          15.20   45.60
2013SO00001   02         3          9.40    28.20
2013SO00001   03         1          11.50   11.50
2013SO00001   04         2          11.55   23.10
2013SO00001   05         2          14.30   28.60
2013SO00001   06         10         13.32   133.20
2013SO00001   07         5          19.42   97.10
2013SO00001   08         6          10.15   60.90
2013SO00001   09         4          12.15   48.60
2013SO00001   10         2          18.88   37.76

Now our results are sorted as expected. Once again we have rolled back the transaction so we are left with only 9 rows in our #SO_Details table and single digit SO_Detail values.

Let’s complicate our example just a bit and add a table of transactions we’d like to process, consisting of inserts, updates and deletes and see if we can do the whole thing with a MERGE. First, the transactions table and some sample transactions.

CREATE TABLE #SO_Trans
(
    [action]            CHAR(1)
    ,SO_Number          VARCHAR(11)
    ,SO_Detail          VARCHAR(4)
    ,Quantity           INT
    ,Price              MONEY
);
INSERT INTO #SO_Trans
    (
        [action], SO_Number, SO_Detail, Quantity, Price
    )
VALUES ('U', '2013SO00001', '5', 15, 12.23)
    ,('I', '2013SO00001', NULL, 4, 16.15)
    ,('I', '2013SO00001', NULL, 3, 12.22)
    ,('D', '2013SO00001', '3', NULL, NULL);
SELECT *
FROM #SO_Trans;
-- Results:
action SO_Number     SO_Detail   Quantity   Price
U      2013SO00001   5           15         12.23
I      2013SO00001   NULL        4          16.15
I      2013SO00001   NULL        3          12.22
D      2013SO00001   3           NULL       NULL

For INSERTs represented in the transactions table, we do not need an SO_Detail number. For DELETEs we do not need Quantity or Price. For UPDATEs we need all three. In order to do a MERGE, we need to consider the following:

  • What is the target table?
  • What is the source table?
  • What are the matching criteria and what operations are performed on which kinds of matches?

The target table is easy, although not so easy as just the #SO_Details table because we need to only include SOs that are in the #SO_Trans table, so we’ll start with that:

-- Target table
SELECT SO_Number, SO_Detail, Quantity, Price
FROM #SO_Details
WHERE SO_Number IN(SELECT SO_Number FROM #SO_Trans);

We’ll put the target table into a CTE for use in the MERGE. Coincidentally the results are the 9 original rows we put into the table, because there is only one SO_Number in the #SO_Trans table. We do need to include all rows for that SO because any of them may be subject to an UPDATE to change the SO_Detail number.

MERGE throws an exception if you try to update any row in the target table more than once, so we need to carefully construct the source table so that there is one and only one matching row for each row in the target.

WITH Target4MERGE AS
(
    SELECT SO_Number, SO_Detail, Quantity, Price
    FROM #SO_Details
    WHERE SO_Number IN(SELECT SO_Number FROM #SO_Trans)
)
SELECT [action]=ISNULL(b.[action], 'U')
    ,SO_Number=ISNULL(a.SO_Number, b.SO_Number)
    ,a.SO_Detail
    ,Quantity=ISNULL(b.Quantity, a.Quantity)
    ,Price=ISNULL(b.Price, a.Price)
FROM Target4MERGE a
FULL JOIN #SO_Trans b
ON a.SO_Number = b.SO_Number AND a.SO_Detail = b.SO_Detail;
-- Results:
action  SO_Number    SO_Detail  Quantity  Price
I       2013SO00001  NULL       4         16.15
I       2013SO00001  NULL       3         12.22
U       2013SO00001  1          3         15.20
U       2013SO00001  2          3         9.40
D       2013SO00001  3          1         11.50
U       2013SO00001  4          2         11.55
U       2013SO00001  5          15        12.23
U       2013SO00001  6          10        13.32
U       2013SO00001  7          5         19.42
U       2013SO00001  8          6         10.15
U       2013SO00001  9          4         12.15

Notes about these results:

  • The two detail lines to be INSERTed have a NULL SO_Detail number because we have not yet determined what it is to be. We’ll be trying to add them at the end.
  • Note the Quantity and Price (15 and 12.23) for SO_Detail=5, which are the values we want to UPDATE.

The key to the matching will be to construct a ROW_NUMBER() that we can use as the new SO_Detail number. So let’s add that:

WITH Target4MERGE AS
(
    SELECT SO_Number, SO_Detail, Quantity, Price
    FROM #SO_Details
    WHERE SO_Number IN(SELECT SO_Number FROM #SO_Trans)
),
    PreSource AS
(
    SELECT [action], SO_Number, SO_Detail, Quantity, Price
        ,r=ROW_NUMBER() OVER
            (
                PARTITION BY SO_Number
                    ,CASE [action] WHEN 'D' THEN 0 ELSE 1 END
                ORDER BY CASE
                         WHEN SO_Detail IS NULL
                         THEN 10000
                         ELSE SO_Detail
                         END
            )
    FROM
    (
        SELECT [action]=ISNULL(b.[action], 'U')
            ,SO_Number=ISNULL(a.SO_Number, b.SO_Number)
            ,a.SO_Detail
            ,Quantity=ISNULL(b.Quantity, a.Quantity)
            ,Price=ISNULL(b.Price, a.Price)
        FROM Target4MERGE a
        FULL JOIN #SO_Trans b
        ON a.SO_Number = b.SO_Number AND a.SO_Detail = b.SO_Detail
    ) a
)
SELECT [action], SO_Number, SO_Detail, Quantity, Price, r
    ,m=MAX(r) OVER
        (
            PARTITION BY SO_Number, CASE [action] WHEN 'D' THEN 0 ELSE 1 END
        )
FROM PreSource;
-- Results:
action  SO_Number    SO_Detail  Quantity  Price  r   m
D       2013SO00001  3          1         11.50  1   1
U       2013SO00001  1          3         15.20  1   10
U       2013SO00001  2          3         9.40   2   10
U       2013SO00001  4          2         11.55  3   10
U       2013SO00001  5          15        12.23  4   10
U       2013SO00001  6          10        13.32  5   10
U       2013SO00001  7          5         19.42  6   10
U       2013SO00001  8          6         10.15  7   10
U       2013SO00001  9          4         12.15  8   10
I       2013SO00001  NULL       4         16.15  9   10
I       2013SO00001  NULL       3         12.22  10  10

Additional comments on the latest query and results:

  • The PARTITION we created for our ROW_NUMBER() separates the record to be deleted from the remainder.
  • In the ORDER BY for our ROW_NUMBER() we substituted 10000 (one more than the maximum number of rows we can have per SO detail line) to make the INSERTed records move to the bottom.
  • We enclosed this query in a CTE, because we also want to use the MAX window aggregate function.
  • For that, the PARTITION is identical to what we used for ROW_NUMBER().
  • The result in the r column (except for the [action]=’D’ row) is the new SO_Detail number.
  • The result in m will be used when we convert our integers to characters with leading numbers.

All that’s left is to construct the final MERGE statement, so here it is.

WITH Target4MERGE AS
(
    SELECT SO_Number, SO_Detail, Quantity, Price
    FROM #SO_Details
    WHERE SO_Number IN(SELECT SO_Number FROM #SO_Trans)
),
    PreSource AS
(
    SELECT [action], SO_Number, SO_Detail, Quantity, Price
        ,r=ROW_NUMBER() OVER
            (
                PARTITION BY SO_Number
                    ,CASE [action] WHEN 'D' THEN 0 ELSE 1 END
                ORDER BY CASE
                         WHEN SO_Detail IS NULL
                         THEN 10000
                         ELSE SO_Detail
                         END
            )
    FROM
    (
        SELECT [action]=ISNULL(b.[action], 'U')
            ,SO_Number=ISNULL(a.SO_Number, b.SO_Number)
            ,a.SO_Detail
            ,Quantity=ISNULL(b.Quantity, a.Quantity)
            ,Price=ISNULL(b.Price, a.Price)
        FROM Target4MERGE a
        FULL JOIN #SO_Trans b
        ON a.SO_Number = b.SO_Number AND a.SO_Detail = b.SO_Detail
    ) a
),
    Source4MERGE AS
(
    SELECT [action], SO_Number, SO_Detail, Quantity, Price, r
        ,m=1+FLOOR(LOG10(
            MAX(r) OVER
                (
                    PARTITION BY SO_Number
                        ,CASE [action] WHEN 'D' THEN 0 ELSE 1 END
                )))
    FROM PreSource
)
MERGE Target4MERGE t
USING Source4MERGE s
ON s.SO_Number = t.SO_Number AND s.SO_Detail = t.SO_Detail
WHEN MATCHED AND [action] = 'D'
THEN DELETE
WHEN MATCHED AND [action] = 'U'
THEN UPDATE
SET SO_Detail = RIGHT(POWER(10, m) + s.r, m)
    ,Quantity = s.Quantity
    ,Price    = s.Price
WHEN NOT MATCHED AND [action] = 'I'
THEN INSERT 
    (
        SO_Number, SO_Detail, Quantity, Price
    )
VALUES 
    (
        s.SO_Number
        ,RIGHT(POWER(10, m) + s.r, m)
        ,s.Quantity
        ,s.Price
    );
SELECT *
FROM #SO_Details;
-- Results:
SO_Number    SO_Detail  Quantity  Price   Extended_Price
2013SO00001  01         3         15.20   45.60
2013SO00001  02         3         9.40    28.20
2013SO00001  03         2         11.55   23.10
2013SO00001  04         15        12.23   183.45
2013SO00001  05         10        13.32   133.20
2013SO00001  06         5         19.42   97.10
2013SO00001  07         6         10.15   60.90
2013SO00001  08         4         12.15   48.60
2013SO00001  09         4         16.15   64.60
2013SO00001  10         3         12.22   36.66

Now that’s one heck of a complicated looking query and it could probably do with some comments, but you can get those as needed from the descriptions I’ve provided. A few more additional notations may be warranted.

  • We applied our 1+FLOOR+LOG10 formula around the MAX() window aggregate function to save a bit of complexity when assigning SO_Detail in the MERGE.
  • The MERGE uses MATCHED twice, segregating DELETEs from UPDATEs based on [action].
  • The NOT MATCHED clause INSERTs the new rows specified by the #SO_Trans table.

Conclusions

You may be asking why not save all of this computational convolution and just always use a 4 digit SO_Detail value with as many leading zeroes as necessary because that’s pretty easy with something like:

SELECT RIGHT('0000'+CAST(SO_Detail AS VARCHAR(4)), 4);

The answer is, yes we could have done that if our business requirements allowed for always having 4 digits in the SO_Detail column. But frankly, this was much more fun and we got to learn a little about logarithms along the way. And I did warn you by calling this article Stupid T-SQL Tricks, now didn’t I?

Still we may have learned a few things, like how to calculate factorials, creating a pseudo-PROD() aggregate, how base 10 logarithms can be used to find the number of digits in a number and how to methodically construct a complex MERGE query one step at a time. All in all, that alone probably made it worthwhile for me to post this blog.

Perhaps it wasn’t quite as entertaining as Johnny Carson’s Stupid Pet Tricks skits, but we hope you enjoyed it anyway.

Follow me on Twitter: @DwainCSQL

Copyright © Dwain Camps 2014 All Rights Reserved

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating