2010-02-24 68 views

回答

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. 
+0

在子選擇中放置「DISTINCT」語句和「ORDER BY」子句應該使該解決方案有效。 – Sonny

+0

對不起mjv,我當然應該更具體。我需要表中存在的每24小時的總計數。 因此,例如,我需要每行的總數,每個ip地址每天最多10行,例如星期一,星期二,星期三等等。 確實有意義或者我是揮霍無度的只對自己有意義? :) – David

+0

@David,看看我的編輯(除了增加每天計算總數的支持外,我還添加了我最初忘記的GROUP BY ......)。如果你對某一天的日子感興趣,而不是單日那天,只需通過DAYOFWEEK(date_voted)改變'YEAR(date_voted),DAYOFYEAR(date_voted)' – mjv

0

假設:只有前十票的隊伍(在投票表中每一行是TEAM_ID投票)從給定的IP地址應該給定日期計算。

所以這裏是每團隊每天的原始投票。

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

現在,使用,糾正投票下來的不超過十號與CASE表達式:

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