如何每天为每个IP地址获取一条不同的记录?

时间:2012-02-28 22:46:09

标签: sql-server tsql

我们正试图限制某些摇滚视频每天投票一个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 个答案:

答案 0 :(得分: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;