Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating Z-Score for each row in MySQL? (simple)

Tags:

mysql

I am looking for an efficient way to assign a Z-Score (also known as standard score) for each row in a MySQL table.

enter image description here

Z = Z-Score
X = Actual value
μ = Mean value
σ = Standard Deviation

I tried:

SELECT pTime,(M1-AVG(M1))/STD(M1),
             (M2-AVG(M2))/STD(M2),
             (M3-AVG(M3))/STD(M3),
             (M4-AVG(M4))/STD(M4)
 FROM mergebuys;

but ended up with only 1 row.

It seems really inefficient to have to use a subquery when it only needs to be calculated one time.

like image 645
Mike Furlender Avatar asked Nov 15 '11 07:11

Mike Furlender


People also ask

How do you find the z-score of a row?

How do you calculate the z-score? The formula for calculating a z-score is is z = (x-μ)/σ, where x is the raw score, μ is the population mean, and σ is the population standard deviation. As the formula shows, the z-score is simply the raw score minus the population mean, divided by the population standard deviation.

How do I find the z-score in SQL Server?

The Z-score normalized value equals (the original column value minus the mean column value) divided by the standard deviation of the column values. We will use T-SQL's AVG() function to calculate the mean and the STDEV() function to calculate the standard deviation.

How do you find the z-score for a set of numbers?

A z score is unique to each value within a population. To find a z score, subtract the mean of a population from the particular value in question, then divide the result by the population's standard deviation.


2 Answers

SELECT
    mergebuys.pTime,
    (mergebuys.M1 - aggregates.AVGM1) / aggregates.STDM1 AS z1,
    (mergebuys.M2 - aggregates.AVGM2) / aggregates.STDM2 AS z2,
    (mergebuys.M3 - aggregates.AVGM3) / aggregates.STDM3 AS z3,
    (mergebuys.M4 - aggregates.AVGM4) / aggregates.STDM4 AS z4
FROM
    mergebuys
    CROSS JOIN (
        SELECT
            AVG(M1) AS AVGM1,
            STD(M1) AS STDM1,
            AVG(M2) AS AVGM2,
            STD(M2) AS STDM2,
            AVG(M3) AS AVGM3,
            STD(M3) AS STDM3,
            AVG(M4) AS AVGM4,
            STD(M4) AS STDM4
        FROM
            mergebuys
    ) AS aggregates
like image 114
Hammerite Avatar answered Oct 28 '22 15:10

Hammerite


Because you are using Aggregate Functions.

For example, If you are using Min or Max function, then there can be only one Minimum or Maximum value in a table for particular column. Same is the case for AVG function.

Thats why aggregate functions are commonly used with Group By clause.

like image 41
Zohaib Avatar answered Oct 28 '22 14:10

Zohaib