带有联接的SQL查询计数每个记录按计数排序的多个结果

时间:2012-10-11 18:29:05

标签: mysql sql statistics analytics

我有一张名为Request的表 其他表通过请求ID链接到Request表。 有一个TwitterTweet表和一个FacebookPost表。 因此,单个请求可以包含50个TwitterTweets和/或20个FacebookPosts或任意数量的推文/帖子 我们可以将它们加在一起,总计70个。

我正在尝试创建一个查询,告诉我具有最高总数的请求是什么。

我知道这是错的: (我试图按照TwitterTweet中的计数来排序它们,但它不会让我做一个我认为的外部连接 会带回Count.count列。它迫使我做一个Left Join来编译。我的逻辑是如此加入 由请求者计算每行的结果

SELECT r1.`id` AS requestid, r1 . * 
FROM  `Request` AS r1
LEFT JOIN 

(SELECT COUNT( * ) AS count, rid
FROM 

((SELECT  `TwitterTweet`.`id` AS  `smid` ,  `TwitterTweet`.`requestid` AS rid
FROM  `TwitterTweet` 
WHERE  `TwitterTweet`.`requestid` = requestid
AND  `TwitterTweet`.`active` =1) AS talias

)) AS Count ON ( Count.rid = requestid ) 
ORDER BY Count.count

*当我尝试添加Facebook方面时,它将不再编译 (概念是从TwitterTweet添加结果来自FacebookPost的结果 附加到特定请求的,这将给我们一个计数。整个结果 应按该计数订购)

SELECT r1.`id` AS requestid, r1 . * 
FROM  `Request` AS r1
LEFT JOIN 

(SELECT COUNT( * ) AS count, rid
FROM 

((SELECT  `TwitterTweet`.`id` AS  `smid` ,  `TwitterTweet`.`requestid` AS rid
FROM  `TwitterTweet` 
WHERE  `TwitterTweet`.`requestid` = requestid
AND  `TwitterTweet`.`active` =1 ) AS talias 

UNION All

(SELECT `FacebookPost`.`id` AS  `smid`, `FacebookPost`.`requestid` AS rid
FROM  `FacebookPost`
WHERE  `FacebookPost`.`requestid` = requestid
AND  `FacebookPost`.`active` = 1) as falias 

)) AS Count ON ( Count.rid = requestid ) 
ORDER BY Count.count

我更新了Query,尝试添加别名:

SELECT rid, SUM(count) total_count

FROM 

(
(SELECT COUNT(*) AS count, r.rid
FROM   request r
       JOIN TwitterTweet tt
       ON r.id = tt.requestid
WHERE  tt.active = 1
GROUP BY r.rid) AS twitter

UNION ALL

(SELECT COUNT(*) AS count, r.rid
FROM   request r
       JOIN FacebookPost fp
       ON r.id = fp.requestid
WHERE  fp.active = 1
GROUP BY r.rid ) AS fbook
)

GROUP BY rid

ORDER BY SUM(count) DESC

我做了另一个调整,给中间子查询一个别名,但现在我只返回一行,在rid列中返回零,在total_count列中返回5686 ...... 5686可能是所有结果。

SELECT counts.rid, SUM(count) total_count

FROM 

(
SELECT COUNT(*) AS count, r.requestid AS rid
FROM   request r
       JOIN TwitterTweet tt
       ON r.id = tt.requestid
WHERE  tt.active = 1
GROUP BY r.requestid

UNION ALL

SELECT COUNT(*) AS count, r.requestid AS rid
FROM   request r
       JOIN FacebookPost fp
       ON r.id = fp.requestid
WHERE  fp.active = 1
GROUP BY r.requestid
) AS counts

GROUP BY counts.rid

ORDER BY SUM(count) DESC

得到了!!! 感谢您的帮助,我不得不根据请求删除这些联接:

SELECT counts.rid, SUM(count) total_count

FROM 

(
SELECT COUNT(*) AS count, tt.requestid AS rid
FROM  TwitterTweet tt
WHERE  tt.active = 1
GROUP BY tt.requestid

UNION ALL

SELECT COUNT(*) AS count, fp.requestid AS rid
FROM   FacebookPost fp
WHERE  fp.active = 1
GROUP BY fp.requestid
) AS counts

GROUP BY counts.rid

ORDER BY SUM(count) DESC

1 个答案:

答案 0 :(得分:1)

SELECT id, SUM(count) total_count

FROM 

(
SELECT COUNT(*) AS count, r.id
FROM   request r
       JOIN TwitterTweet tt
       ON r.id = tt.requestid
WHERE  tt.active = 1
GROUP BY r.id

UNION ALL

SELECT COUNT(*) AS count, r.id
FROM   request r
       JOIN FacebookPost fp
       ON r.id = fp.requestid
WHERE  fp.active = 1
GROUP BY r.id
) sub

GROUP BY id

ORDER BY SUM(count) DESC
;