23.5. Average Deviation

If you have a version of SQL with an absolute value function, ABS(), you can also compute the average deviation following this pattern:

BEGIN
SELECT AVG(x) INTO :average FROM Samples;
SELECT SUM(ABS(x - :average)) / COUNT(x) AS AverDeviation
  FROM Samples;
END;

This is a measure of how far data values drift away from the average, without any consideration of the direction of the drift.

Get Joe Celko's SQL for Smarties, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.