0

我想创建一个加权使用排名/流行度查询(或批量更新,如果查询证明实时使用很费力!)但我一直在画一个空白。希望您对如何执行此操作有更好的想法。

我已经简化了我的数据库以帮助说明问题(见下图!)基本上,当用户通过标签选择特定博客时,我会在 TagLog 表中添加一个条目。假设此示例的博客和标签的集合保持静态。假设上述情况,我想做以下事情:

  • 查找任何给定标签的前 10 个博客
  • 查找任何给定标签和用户的前 10 个博客

真正的困难来自这样一个事实,即我想对结果进行加权,以便最近的 TagLog 条目具有更大的意义。

在这方面的任何帮助将不胜感激!谢谢...

数据库图

4

1 回答 1

1

这应该让您前往有用的地方:

-- Sample data.
declare @Blogs as Table ( BlogId Int Identity, URL VarChar(256) )
insert into @Blogs ( URL ) values
  ( 'www.google.com' ), ( 'www.java.com' )

declare @Tags as Table ( TagId Int Identity, BlogId Int, Tag VarChar(64) )
insert into @Tags ( BlogId, Tag ) values
  ( 1, 'Not Evil' ), ( 2, 'Buggy' )

declare @TagLog as Table ( TagId Int, UserGuid UniqueIdentifier, Visited DateTime )
insert into @TagLog ( TagId, UserGuid, Visited ) values
  ( 1, NewId(), '20130502' ), ( 1, NewId(), '20130508' ), ( 1, NewId(), '20130515' ),
  ( 2, NewId(), '20130501' ), ( 2, NewId(), '20130508' ), ( 2, NewId(), '20130515' )

declare @Now as DateTime = '20130516' -- Test value.

-- Display all sample data.
select *, DateDiff( day, TL.Visited, @Now ) as Age -- Use appropriate units, e.g. week, minute.
  from @Blogs as B inner join
    @Tags as T on T.BlogId = B.BlogId inner join
    @TagLog as TL on TL.TagId = T.TagId

-- Compute a weight based on age.
--   Use the reciprocal of the age so that newer visits have higher weight.
--   Add 1.0 to avoid divide by zero errors.
select T.TagId, Count( 42 ) as Visits, Sum( 1.0 / ( DateDiff( day, TL.Visited, @Now ) + 1.0 ) ) as AgeWeight
  from @Blogs as B inner join
    @Tags as T on T.BlogId = B.BlogId inner join
    @TagLog as TL on TL.TagId = T.TagId
  group by T.TagId
于 2013-05-15T15:12:26.753 回答