SQRT: from Tableau to SQL

Returns the nonnegative square root. Below, 8 sample rows go through the calculation and through the SQL, and both return the same numbers.

Calculated field

SQRT([amount])

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 sqrt(amount) ELSE NULL END AS result FROM input ORDER BY id

Runs the example above.

Result

idamountunitsscoreresult
1-2.52-0.8null
20000
31.530.251.22474
4440.52
5950.83
616614
7null7nullnull
848-0.52

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

Where a naive translation goes wrong

  • Replacing null results with zero changes missing-value semantics.

    Naive SQL

    SELECT coalesce((CASE WHEN amount>=0 THEN sqrt(amount) ELSE NULL END),0) AS result FROM input ORDER BY id
    idamountunitsscoreresult
    1-2.52-0.80
    20000
    31.530.251.22474
    4440.52
    5950.83
    616614
    7null7null0
    848-0.52

    Matches the calculation

    SELECT CASE WHEN amount>=0 THEN sqrt(amount) ELSE NULL END AS result FROM input ORDER BY id
    idamountunitsscoreresult
    1-2.52-0.8null
    20000
    31.530.251.22474
    4440.52
    5950.83
    616614
    7null7nullnull
    848-0.52

Signature

SQRT(number)