Seems like this is quite interesting topic, I have another extremely valuable comment. Joe Celko – I really don’t think his name needs any introduction hereJ – sent me another suggestion for an improvement of Steve’s version – use the standard POWER() function to return the Age column to a specific power instead of calculating the value manually.
This way the compiler writer can handle all that floating point error stuff for us. So here is the corrected solution for both, Skewness and Kurtosis. BTW, personally I prefer the CLR solution. It performs the same as the best T-SQL solution and once it is done, it is the easiest to use.
— Joe Celko correction – use POWER() function – Skewness
SELECT
(rx3 – 3*rx2*av + 3*rx*av*av – rn*av*av*av)
/ (stdv*stdv*stdv) * rn / (rn–1) / (rn–2)
FROM (
SELECT
SUM(Age) AS rx,
SUM(POWER(Age,2)) AS rx2,
SUM(POWER(Age,3)) AS rx3,
COUNT(Age) AS rn,
STDEV(Age) AS stdv,
AVG(Age) AS av
FROM (SELECT 1e0*Age AS Age FROM vTargetMail) AS s
) AS s
— Joe Celko correction – use POWER() function – Kurtosis
SELECT
(rx4 – 4*rx3*av + 6*rx2*av*av – 4*rx*av*av*av + rn*av*av*av*av)
/ (stdv*stdv*stdv*stdv) * rn * (rn+1) / (rn–1) / (rn–2) / (rn–3)
– 3e0 * (rn–1) * (rn–1) / (rn–2) / (rn–3)
FROM (
SELECT
SUM(Age) AS rx,
SUM(POWER(Age,2)) AS rx2,
SUM(POWER(Age,3)) AS rx3,
SUM(POWER(Age,4)) AS rx4,
COUNT(Age) AS rn,
STDEV(Age) AS stdv,
AVG(Age) AS av
FROM (SELECT 1e0*Age AS Age FROM vTargetMail) AS s
) AS s