SUM(expression) adds every non-null value in the group. In SQL it is the same word: sum(Sales) with a GROUP BY for the view's dimensions. A group where every value is null returns null, not 0. The thing to get right is where SUM sits in a ratio: SUM([Sales]) / SUM([Units]) is not the sum of each row's ratio.
Updated · Checked on the xmetric engine
What is the syntax?
SUM(expression)
expression
a numeric field or row-level calculation, such as [Sales] or [Price] * [Quantity].
Returns a number: the total of the non-null values. Null when the group has no non-null value.
What does it return, and what is the SQL?
Each example runs on the same 12 sample rows twice: the Tableau formula on the xmetric engine, and the SQL in DuckDB.
What are total sales?
Tableau
SUM([Sales])
SQL (DuckDB)
SELECTsum(Sales) AS result
FROM input
With no dimension in the view there is one mark: 1,630. Line 2 and the two North lines have no sales and add nothing.
Result
1,630
✓ The Tableau formula and the SQL return the same 1 rows.
What are total sales per region?
Tableau
SUM([Sales])
SQL (DuckDB)
SELECT Region, sum(Sales) AS result
FROM input
GROUPBY Region
ORDERBY Region
One mark per region. North has two order lines and no sales value on either, so its total is null rather than 0.
Region
Result
East
550
North
null
South
80
West
1,000
✓ The Tableau formula and the SQL return the same 4 rows.
What is the average price per unit in each region?
Tableau
SUM([Sales]) /SUM([Units])
SQL (DuckDB)
SELECT Region, sum(Sales) /sum(Units) AS result
FROM input
GROUPBY Region
ORDERBY Region
East sells 550 over 13 units: 42.31 a unit. Each SUM runs over the region first, then the division.
Region
Result
East
42.31
North
null
South
40
West
76.92
✓ The Tableau formula and the SQL return the same 4 rows.
Where does a straight translation go wrong?
Common mistake
Why does summing each row's ratio give a different price?
Tableau evaluates SUM([Sales]) / SUM([Units]) per mark: two totals, then one division. Dividing on each row and summing adds up the per-line prices instead, so East gets 210 rather than 42.31.
Tableau
SUM([Sales]) /SUM([Units])
Tempting SQL
SELECT Region, sum(Sales / Units) AS result
FROM input
GROUPBY Region
ORDERBY Region
SQL that matches
SELECT Region, sum(Sales) /sum(Units) AS result
FROM input
GROUPBY Region
ORDERBY Region
Region
Tableau
Tempting SQL
SQL that matches
East
42.31
210
42.31
North
null
null
null
South
40
40
40
West
76.92
167
76.92
✓ The SQL that matches returns the Tableau result on every row; the marked cells are where the tempting SQL differs.
Common mistake
Why does North show 0 in SQL but null in Tableau?
SUM ignores nulls, and a group with nothing left to add returns null. Wrapping the SQL in coalesce(..., 0) out of habit turns North into 0, which a chart then plots as a real zero. Use ZN(SUM([Sales])) in Tableau if you want the zero.
Tableau
SUM([Sales])
Tempting SQL
SELECT Region, coalesce(sum(Sales), 0) AS result
FROM input
GROUPBY Region
ORDERBY Region
SQL that matches
SELECT Region, sum(Sales) AS result
FROM input
GROUPBY Region
ORDERBY Region
Region
Tableau
Tempting SQL
SQL that matches
East
550
550
550
North
null
0
null
South
80
80
80
West
1,000
1,000
1,000
✓ The SQL that matches returns the Tableau result on every row; the marked cells are where the tempting SQL differs.
What does SUM return for a group with only nulls?
North has two order lines and no sales value on either. Both Tableau and SQL return null there. Each result column is one mark, by Region.
Case
Tableau, then SQL (DuckDB)
East
North
South
West
all-null group
SUM([Sales])Region, sum(Sales)
550
null
80
1,000
✓ 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
SELECT Region, SUM(Sales) AS result
FROM input
GROUPBY Region
ORDERBY Region