我有两个查询(返回正确的结果),我基本上想要合并计数然后分组:
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 = postingBidID。我尝试了以下但无济于事:
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 COUNT(*):10,1,1,13,4,3
我需要的是:
profileID:1,2,3 COUNT(*):23,5,4
答案 0 :(得分: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