[a] / [b] divides and returns a decimal even when both are integers: 7 / 2 is 3.5. DuckDB's / does the same, but many databases truncate integer division, so cast there. For ratios, keep Tableau's shape: SUM([a]) / SUM([b]) is not the average of row ratios.
Updated · Checked on the xmetric engine
What is the syntax?
a / b
a, b
numbers, numeric fields or aggregates.
Returns a decimal number. Null if either side is null.
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.
What is a / b on each row?
Tableau
[a]/[b]
SQL (DuckDB)
SELECT a / b AS result
FROM input
ORDERBY id
7 / 2 is 3.5, not 3.
a
b
Result
7
2
3.5
−7
2
−3.5
2
5
0.4
9
4
2.25
5
3
1.67
2
null
1
null
−3
−2
1.5
✓ The Tableau formula and the SQL return the same 8 rows.
What is the ratio of the totals?
Tableau
SUM([a]) /SUM([b])
SQL (DuckDB)
SELECTsum(a) /sum(b) AS result
FROM input
Total a (14) over total b (16).
Result
0.88
✓ The Tableau formula and the SQL return the same 1 rows.
Where does a straight translation go wrong?
Common mistake
Why is avg(a / b) not the ratio of the totals?
Averaging row ratios weights every row equally; SUM([a]) / SUM([b]) weights them by b. They give 0.97 and 0.875 here.
Tableau
SUM([a]) /SUM([b])
Tempting SQL
SELECTavg(a / b) AS result
FROM input
SQL that matches
SELECTsum(a) /sum(b) AS result
FROM input
Tableau
Tempting SQL
SQL that matches
0.88
0.97
0.88
✓ The SQL that matches returns the Tableau result on every row; the marked cells are where the tempting SQL differs.
Common mistake
Why does floor(a / b) lose the fraction?
That's what integer division does in many databases. Tableau's / keeps 3.5; use DIV when you want the whole part.
Tableau
[a]/[b]
Tempting SQL
SELECTfloor(a / b) AS result
FROM input
ORDERBY id
SQL that matches
SELECT a / b AS result
FROM input
ORDERBY id
a
b
Tableau
Tempting SQL
SQL that matches
7
2
3.5
3
3.5
−7
2
−3.5
−4
−3.5
2
5
0.4
0
0.4
9
4
2.25
2
2.25
5
3
1.67
1
1.67
2
null
null
null
1
null
null
null
−3
−2
1.5
1
1.5
✓ The SQL that matches returns the Tableau result on every row; the marked cells are where the tempting SQL differs.
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
SELECTDIV0NULL(a, b) AS result
FROM input
ORDERBY id