DIV: from Tableau to SQL

Returns the integer quotient, truncated toward zero. Below, 8 sample rows go through the calculation and through the SQL, and both return the same numbers.

Calculated field

DIV([units], 3)

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 trunc(units/3) AS result FROM input ORDER BY id

Runs the example above.

Result

idamountunitsscoreresult
1-2.52-0.80
20000
31.530.251
4440.51
5950.81
616612
7null7null2
848-0.52

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

Where a naive translation goes wrong

  • Floating-point division retains a fractional quotient.

    Naive SQL

    SELECT units/3 AS result FROM input ORDER BY id
    idamountunitsscoreresult
    1-2.52-0.80.666667
    20000
    31.530.251
    4440.51.33333
    5950.81.66667
    616612
    7null7null2.33333
    848-0.52.66667

    Matches the calculation

    SELECT trunc(units/3) AS result FROM input ORDER BY id
    idamountunitsscoreresult
    1-2.52-0.80
    20000
    31.530.251
    4440.51
    5950.81
    616612
    7null7null2
    848-0.52

Signature

DIV(integer, integer)