我想创建一个加权使用排名/热门程度查询(或批量更新,如果查询证明是非常费力的实时使用!)但我一直在画一个空白。希望你能更好地了解如何做到这一点。
我已经简化了我的数据库以帮助说明问题(参见下面的图表!)基本上,当用户通过标签选择特定博客时,我会在TagLog表中添加一个条目。假设此示例中博客和标签的集合保持静态。假设上述情况,我想做以下事情:
真正的困难来自这样一个事实,即我想对结果进行加权,以便更新的TagLog条目具有更大的意义。
在这方面的任何帮助将不胜感激!感谢...
答案 0 :(得分: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