COT: from Tableau to SQL

Returns the cotangent of an angle in radians. Below, 8 sample rows go through the calculation and through the SQL, and both return the same numbers.

Calculated field

COT([score])

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 score=0 THEN NULL ELSE 1/tan(score) END AS result FROM input ORDER BY id

Runs the example above.

Result

idamountunitsscoreresult
1-2.52-0.8-0.971215
2000null
31.530.253.91632
4440.51.83049
5950.80.971215
616610.642093
7null7nullnull
848-0.5-1.83049

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 score=0 THEN NULL ELSE 1/tan(score) END),0) AS result FROM input ORDER BY id
    idamountunitsscoreresult
    1-2.52-0.8-0.971215
    20000
    31.530.253.91632
    4440.51.83049
    5950.80.971215
    616610.642093
    7null7null0
    848-0.5-1.83049

    Matches the calculation

    SELECT CASE WHEN score=0 THEN NULL ELSE 1/tan(score) END AS result FROM input ORDER BY id
    idamountunitsscoreresult
    1-2.52-0.8-0.971215
    2000null
    31.530.253.91632
    4440.51.83049
    5950.80.971215
    616610.642093
    7null7nullnull
    848-0.5-1.83049

Signature

COT(number)