开发者

mySQL AVG - A way to merge two results for AVG

开发者 https://www.devze.com 2023-02-21 17:06 出处:网络
I\'m trying to get the average number of interactions on my metrics, but this query gets the average number of inteactions for those who DID interact (those in metricsActions are only there i开发者_St

I'm trying to get the average number of interactions on my metrics, but this query gets the average number of inteactions for those who DID interact (those in metricsActions are only there i开发者_StackOverflow中文版f they HAVE interacted, their metricsID still exists in metrics table).

So, I'm wondering how I can get the average of all those users. I can do two queries, but then how would I average with the two results? Is it possible to merge?

SELECT AVG(`instances`) FROM (SELECT m.`metricID`, COUNT(mc.`metricAction`) as `instances` FROM `metricActions` mc LEFT JOIN `metrics` m ON m.`metricID` = mc.`metricID` WHERE m.`clientID` = '10412' AND `type` = '2' AND GROUP BY mc.`metricID` ORDER BY `instances` DESC ) as `temp`


You should use the LEFT JOIN the other way around:

SELECT  AVG(`instances`)
FROM    (
        SELECT  m.`metricID`, COUNT(mc.`metricAction`) as `instances`
        FROM   `metrics` m
        LEFT JOIN
                `metricActions` mc
        ON      mc.`metricID` = m.`metricID`
                AND mc.`type` = '2'
        WHERE   m.`clientID` = '10412'
        GROUP BY
                m.`metricID`
        ) as `temp`
0

精彩评论

暂无评论...
验证码 换一张
取 消