Oliver
Oliver

Reputation: 839

MySQL AVG query help

I have this query which works perfectly:

SELECT cp.*
FROM CustPrimaryQ cp
    JOIN Customer c ON cp.CxID = c.CustomerID
    JOIN SacCode sc ON sc.SacCode = c.SacCode
WHERE sc.ResellerCorporateID = 392

However I am trying to modify it to calculate an average.

Each row of the CustPrimaryQ table has a field called QScore and it's this field I want to find out the total average of.

In other words if there are 10 rows in CustPrimaryQ I want the Average QScore for the 10 rows.

Any help would be much appreciated.

Upvotes: 0

Views: 93

Answers (1)

Larry Lustig
Larry Lustig

Reputation: 50970

It depends on the relationship among the three tables. But if your current query is guaranteed to return the records you want to average, all you have to do is:

SELECT AVG(QScore)
FROM CustPrimaryQ cp
JOIN Customer c ON cp.CxID = c.CustomerID
JOIN SacCode sc ON sc.SacCode = c.SacCode
WHERE sc.ResellerCorporateID = 392

Upvotes: 5

Related Questions