我有2个表,带有offender_id的报告和带有account_id的游戏。每个帐户都有多个游戏,有些还有针对他们的报告。
我需要能够返回每个帐户报告的游戏百分比。
游戏
account_id
1
1
1
2
2
2
3
3
报告
offender_id
1
2
2
3
3
必填报告结果
ID PercentageReported
1 33%
2 66%
3 100%
答案 0 :(得分:2)
如果我理解这一点,那么也许每场比赛都记录在Reports
表中,每次进攻都记在SELECT temp1.account_id, gamesPlayedCount,
IFNULL(gamesOffendedCount, 0) AS gamesOffendedCount,
CAST(100*IFNULL(gamesOffendedCount, 0)/gamesPlayedCount AS DECIMAL(10, 2))
AS offencePercentage
FROM
(
SELECT g.account_id, COUNT(g.account_id) AS gamesPlayedCount
FROM GamesPlayed g
GROUP BY g.account_id
) AS temp1
LEFT JOIN
(
SELECT r.offender_id, COUNT(r.offender_id) AS gamesOffendedCount
FROM Reports r
GROUP BY r.offender_id
) AS temp2
ON temp1.account_id = temp2.offender_id
?
所以,如果是这种情况,那么查询可能写成如下:
= blog( 'portfolio' ).articles.map { | r | r.data.category }.uniq.sort