我确实有以下分组查询:
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;
输出与我想要的结构相匹配。
然而,数据中的某些帖子每个帖子只有一个变体。我只想计算帖子有多种变化的帖子和变体。我想到了一个类似的查询:
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仅适用于整体分组,我不确定如何将其应用于子分组。我也为每个帖子获得了多行(这很明显,因为我添加了第二个分组)
谢谢我非常感谢您的专业知识和帮助!
答案 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;
编辑:尝试这个只访问主表一次的优化查询。
但是,此查询假定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;
希望这有帮助。