DIV(integer1, integer2) divides and keeps the integer part: DIV(11, 2) is 5 and DIV(-7, 2) is -3, because the fraction is cut toward zero. In DuckDB that is trunc(a / b). Floor division (fdiv, or Python's //) agrees on positive numbers but gives -4 for -7 / 2.
Updated · Checked on the xmetric engine
What is the syntax?
DIV(integer1, integer2)
integer1
the dividend, an integer.
integer2
the divisor, an integer.
Returns an integer: integer1 divided by integer2, with the fraction dropped toward zero.
What does it return, and what is the SQL?
Each example runs on the same 8 sample rows twice: the Tableau formula on the xmetric engine, and the SQL in DuckDB.
How many whole pairs are in each quantity?
Tableau
DIV([Qty], 2)
SQL (DuckDB)
SELECTtrunc(Qty /2) AS result
FROM input
ORDERBY id
7 makes 3 pairs and 10 makes 5. -7 gives -3: the fraction is dropped toward zero.
Qty
Result
7
3
−7
−3
10
5
9
4
2
1
4
2
−1
0
0
0
✓ The Tableau formula and the SQL return the same 8 rows.
Where does a straight translation go wrong?
Common mistake
Why does floor division give -4 where Tableau gives -3?
DuckDB's fdiv (and // in Python) round the quotient down. DIV cuts toward zero, so -7 / 2 is -3 in Tableau and -4 with floor division; -1 / 2 is 0 and -1.
Tableau
DIV([Qty], 2)
Tempting SQL
SELECTfdiv(Qty, 2) AS result
FROM input
ORDERBY id
SQL that matches
SELECTtrunc(Qty /2) AS result
FROM input
ORDERBY id
Qty
Tableau
Tempting SQL
SQL that matches
7
3
3
3
−7
−3
−4
−3
10
5
5
5
9
4
4
4
2
1
1
1
4
2
2
2
−1
0
−1
0
0
0
0
0
✓ The SQL that matches returns the Tableau result on every row; the marked cells are where the tempting SQL differs.
What does DIV return when the quotient is negative?
Dividing by a negative number flips the sign, and the fraction is still dropped toward zero. Columns 1 to 8 are the sample rows, by id.
Case
Tableau, then SQL (DuckDB)
1
2
3
4
5
6
7
8
negative quotients
DIV([Qty], -4)trunc(Qty / -4)
−1
1
−2
−2
0
−1
0
0
✓ On every row, each Tableau formula and its SQL return the same value.
How do I write it in Snowflake or BigQuery?
The first example, written for each warehouse from its documentation. These are not run here; DuckDB is.
Snowflake
SELECTTRUNC(Qty /2) AS result
FROM input
ORDERBY id