PI: from Tableau to SQL

Returns the ratio of a circle circumference to its diameter. Below, 8 sample rows go through the calculation and through the SQL, and both return the same numbers.

Calculated field

PI()

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 pi() AS result FROM input ORDER BY id

Runs the example above.

Result

idamountunitsscoreresult
1-2.52-0.83.14159
20003.14159
31.530.253.14159
4440.53.14159
5950.83.14159
616613.14159
7null7null3.14159
848-0.53.14159

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

Where a naive translation goes wrong

  • A rounded decimal literal loses precision.

    Naive SQL

    SELECT 3.14 AS result FROM input ORDER BY id
    idamountunitsscoreresult
    1-2.52-0.83.14
    20003.14
    31.530.253.14
    4440.53.14
    5950.83.14
    616613.14
    7null7null3.14
    848-0.53.14

    Matches the calculation

    SELECT pi() AS result FROM input ORDER BY id
    idamountunitsscoreresult
    1-2.52-0.83.14159
    20003.14159
    31.530.253.14159
    4440.53.14159
    5950.83.14159
    616613.14159
    7null7null3.14159
    848-0.53.14159

Signature

PI()