0

我有两个查询(返回正确的结果),我基本上想组合计数然后分组:

Select profileID, 
Count(*) as numTotal from replies
GROUP BY profileID;

结果:

profileID: 1,2,3   
numTotal: 10,1,1

Select postedByID,
Count(*) as numTotal from WIRL_01.posts 
Group by postedByID

结果:

postedByID: 1,2,3 
numTotal: 13,4,3

在这种情况下,我想将 profileID = postedByID 分组。我尝试了以下但无济于事:

Select profileID,
Count(*) as numTotal 
from replies
INNER JOIN posts ON replies.profileID = posts.postedByID
Group by profileID

结果:

postedByID: 1,2,3 
numTotal: 130,4,3

任何指导将不胜感激。

编辑:

这个查询让我更接近一点,但是如何按 profileID 对结果进行分组?

Select profileID, Count(*) from WIRL_01.replies Group by profileID
UNION ALL
Select postedByID, Count(*) from WIRL_01.posts Group by postedByID

给我...

profileID:1,2,3,1,2,3 计数(*):10,1,1,13,4,3

我需要的是:

profileID: 1,2,3 计数(*):23,5,4

4

1 回答 1

0

知道了!:)

Select profileID, sum(count) as totalNum from (Select profileID, Count(*) as count from  replies Group by profileID
UNION ALL
Select postedByID, Count(*) as count from posts Group by postedByID) as numTotal
group by profileID

非常感谢 Joe Stafanelli:Selecting Sum of the Count From Tables in a DB

于 2012-11-11T16:24:33.863 回答