RAWSQLAGG_REAL("AVG(%1)", [Sales]) sends AVG(Sales) to the database once per mark. In SQL, write the aggregate with the column in place of %1 and GROUP BY the view's dimensions. Translate the aggregate exactly: a ratio of sums is not an average of ratios, and AVG skips nulls.
SELECT Region, sum(Sales) /sum(Units) AS result
FROM input
GROUPBY Region
ORDERBY Region
%1 is Sales, %2 is Units. Both sums run over the same rows, including East's line with no sales value.
Region
Result
East
13.56
North
null
West
39.19
✓ The Tableau formula and the SQL return the same 3 rows.
Where does a straight translation go wrong?
Common mistake
Why is the average of line prices different from the region price?
The text divides two totals. Averaging each line's Sales / Units gives every line the same weight, whatever its size, and skips the line with no sales value: East moves from 13.56 to 18.52.
The first argument after the SQL string. %2 is the second, and so on, whatever order the placeholders appear in the text. To translate a RAWSQL call, replace each %n with the column it points at and keep the rest of the text.
Does Tableau check the SQL inside RAWSQL?
No. Tableau passes the text to the database as it is, so a typo or a function your database lacks fails at query time, in the database. The text is also tied to that database's dialect: point the workbook at a different database and the same text can fail or mean something else.
What is the difference between RAWSQL and RAWSQLAGG?
RAWSQL_ functions are row-level: the expression runs once per row. RAWSQLAGG_ functions hold an aggregate such as SUM(%1) and run once per mark, grouped by the dimensions in the view. In SQL the first is a plain column expression; the second needs a GROUP BY on the view's dimensions.