2012-02-28 89 views
1

我們正試圖限制每天一個IP地址對一些搖滾視頻的投票。我已經從堆棧溢出的另一個問題開始了這個查詢。但是,每天只需要每個IP地址一次投票就可以每天返回一張投票。我該怎麼做?每個IP地址每天如何獲得一個不同的記錄?

;with cte as 
(
    select 
    *, 
    row_number() over(partition by datediff(d, 0, created) order by created desc) as rn 
    from rock_vote 
) 
select cte.VideoId, cte.IPAddress, o.BandName, cte.Created 
from cte 
inner join rock_video o on cte.videoId = o.videoid 
where rn = 1 
order by created desc 

回答

1
;with cte as 
(
    select 
    *, 
    row_number() over(partition by IPAddress, datediff(d, 0, created) 
    -------------------------------^^^^^^^^^^^ 
    order by created desc) as rn 
    from rock_vote 
) 
select cte.VideoId, cte.IPAddress, o.BandName, cte.Created 
from cte 
inner join rock_video AS o on cte.videoId = o.videoid 
where cte.rn = 1 
order by o.created desc; 
+0

謝謝Aaron。愛燒杯的化身。 – 2012-02-29 01:21:07

相關問題