聚合具有要考虑的最大行数的SQL查询

时间:2010-02-24 18:38:43

标签: sql mysql group-by

我正在试图找出一个SQLquery。

我有一个包含team_idip_addressdate_voted字段的投票数据表,我需要为每个team_id返回一个投票数,但只计算第一个在任何24小时内每个IP地址10行。

2 个答案:

答案 0 :(得分:0)

没有时间检查,但以下应该可以解决问题。

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.

答案 1 :(得分:0)

假设:从给定的IP地址开始,只有团队的前10张投票(投票表中的每一行都是对team_id的投票)应计入给定日期。

所以这是每队每天的原始投票。

select team_id, vote_date, ip_address, count(*) as raw_vote_count
  from votes
 group by team_id, vote_date, ip_address

现在,使用它,使用CASE表达式将票数减少到不超过10:

select team_id, vote_date, ip_address,
       case when raw_vote_count > 10 
            then 10 
            else raw_vote_count 
        end as adjusted_vote_count
  from (select team_id, vote_date, ip_address, count(*) as raw_vote_count
          from votes
         group by team_id, vote_date, ip_address
       ) sub1

如果你想在白天获得总票数,那就是:

select team_id, sum(adjusted_vote_count)
  from (
       select team_id, vote_date, ip_address,
              case when raw_vote_count > 10 
                   then 10 
                   else raw_vote_count 
               end as adjusted_vote_count
         from (select team_id, vote_date, ip_address, count(*) as raw_vote_count
                 from votes
                group by team_id, vote_date, ip_address
              ) sub1
       )
 where date = :mydate
 group by team_id
 order by team_id