mysql对数据进行分组,其中子分组匹配if子句

时间:2014-09-26 07:53:50

标签: mysql sql

我确实有以下分组查询:

查询

SELECT P.partner, COUNT(DISTINCT P.postid) AS nb_post,
COUNT(DISTINCT P.postvariationid) AS nb_postvariation,
COUNT(DISTINCT P.postvariationid) / COUNT(DISTINCT P.postid) AS avg_postvariation
FROM posts P
GROUP BY P.partner
ORDER BY P.id DESC;

Fiddle here

输出与我想要的结构相匹配。

然而,数据中的某些帖子每个帖子只有一个变体。我只想计算帖子有多种变化的帖子和变体。我想到了一个类似的查询:

SELECT P.partner, COUNT(DISTINCT P.postid) AS nb_post,
COUNT(DISTINCT P.postvariationid) AS nb_postvariation,
COUNT(DISTINCT P.postvariationid) / COUNT(DISTINCT P.postid) AS avg_postvariation
FROM posts P
GROUP BY P.partner,P.postid
HAVING COUNT(DISTINCT P.postvariationid) / COUNT(DISTINCT P.postid) > 1
ORDER BY P.id DESC;

然而,此COUNTS仅适用于整体分组,我不确定如何将其应用于子分组。我也为每个帖子获得了多行(这很明显,因为我添加了第二个分组)

谢谢我非常感谢您的专业知识和帮助!

1 个答案:

答案 0 :(得分:0)

以下查询应该适合您

SELECT  P.partner
        ,COUNT(DISTINCT P.postid) AS nb_post
        ,COUNT(DISTINCT P.postvariationid) AS nb_postvariation
        ,COUNT(DISTINCT P.postvariationid) / COUNT(DISTINCT P.postid) AS avg_postvariation
FROM    posts P 
        INNER JOIN (
            SELECT  P.postid
                    ,COUNT(DISTINCT P.postvariationid) AS nb_postvariation
            FROM posts P
            GROUP BY P.postid
            HAVING COUNT(DISTINCT P.postvariationid) > 1
        ) Q
        ON P.postid = Q.postid
GROUP BY P.partner
ORDER BY P.id DESC;

SQLFiddle Demo

编辑:尝试这个只访问主表一次的优化查询。 但是,此查询假定nb_postvariation的值不会在postid

的多个值中重复

为了获得更好的效果,您可以在posts(partner,postid,postvariationid)

上添加覆盖索引
SELECT  P.partner
        ,SUM(nb_post) AS nb_post
        ,SUM(nb_postvariation) AS nb_postvariation
        ,SUM(nb_postvariation)/SUM(nb_post) AS nb_postvariation
FROM    (
        SELECT  P.partner,P.postid
                ,COUNT(DISTINCT P.postid) AS nb_post
                ,COUNT(DISTINCT P.postvariationid) AS nb_postvariation
        FROM    posts P  
        GROUP BY P.partner,P.postid 
        HAVING COUNT(DISTINCT P.postvariationid) > 1
       )P;

SQLFiddle Demo

希望这有帮助。