我正在尝试找出一个SQLquery。
我有一个包含team_id
,ip_address
和date_voted
字段的投票数据表,我需要返回每个team_id
的投票计数,但是在任何24小时内仅计算每个IP地址的前10行。
最佳答案
没时间检查,但是以下方法可以解决问题。
SELECT Yr, DoY, team_id, SUM(IF NbVote < 10, NbVote, 10) As FilteredVoteCount
FROM (
SELECT YEAR(date_voted) AS Yr, DAYOFYEAR(date_voted) AS DoY,
team_id,
ip_address,
COUNT(*) AS NbVotes
FROM myTable
-- WHERE here for some possible extra condition.
GROUP BY YEAR(date_voted), DAYOFYEAR(date_voted), team_id, ip_address
)
GROUP BY Yr, DoY, team_id
ORDER BY Yr, DoY, team_id -- or some other order may be desired.
关于sql - 汇总SQL查询时要考虑的最大行数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2328534/