LOG: from Tableau to SQL

Returns a logarithm in the specified base. Below, 8 sample rows go through the calculation and through the SQL, and both return the same numbers.

Calculated field

LOG([amount], 2)

Sample data

idamountunitsscoreother
1-2.52-0.83
20004
31.530.252
4440.58
5950.811
6166114
7null7null9
848-0.5null

The same calculation as SQL

SELECT CASE WHEN amount>0 THEN ln(amount)/ln(2) ELSE NULL END AS result FROM input ORDER BY id

Runs the example above.

Result

idamountunitsscoreresult
1-2.52-0.8null
2000null
31.530.250.584963
4440.52
5950.83.16993
616614
7null7nullnull
848-0.52

Same rows from the calculation and from the SQL in DuckDB.

Where a naive translation goes wrong

  • The default base-ten logarithm differs from the requested base two.

    Naive SQL

    SELECT CASE WHEN amount>0 THEN log10(amount) ELSE NULL END AS result FROM input ORDER BY id
    idamountunitsscoreresult
    1-2.52-0.8null
    2000null
    31.530.250.176091
    4440.50.60206
    5950.80.954243
    616611.20412
    7null7nullnull
    848-0.50.60206

    Matches the calculation

    SELECT CASE WHEN amount>0 THEN ln(amount)/ln(2) ELSE NULL END AS result FROM input ORDER BY id
    idamountunitsscoreresult
    1-2.52-0.8null
    2000null
    31.530.250.584963
    4440.52
    5950.83.16993
    616614
    7null7nullnull
    848-0.52

Signature

LOG(number, [number])