我相信它会工作,你只需要弄清楚你是如何决定用户是否相似。以下给出了一个基于item1与item1s配对的建议字段(反之亦然) - 它不包括用户已拥有的项目。当然,您可以做更复杂的事情,但这里有一些东西可以开始使用
select *, ISNULL((SELECT STUFF
((SELECT ', ' + CAST(ITEM2 AS VARCHAR(10)) [text()] from
((select top 5 ISNULL(item2,'') item2, count(item2) as cnt from items as CountTable1 where item1=Res.item1 and item2 is not null and len(item2) > 0
and item2 not in (select item2 from items where id=Res.id UNION select item1 from items where id=Res.id)
group by item2 order by cnt desc)
UNION
/* Below includes suggestions from item1 */
(select top 5 ISNULL(item1,'') item1, count(item1) as cnt from items as CountTable2 where item2=Res.item1 and item1 is not null and len(item1) > 0
and item1 not in (select item1 from items where id=Res.id UNION select item2 from items where id=Res.id)
group by item1 order by cnt desc))
as Suggs where item1=Res.item1 FOR XML PATH('')
, TYPE)
.value('.','NVARCHAR(MAX)'),1,2,' ')
List_Output)
,'') as Suggestions from items as Res
Sql Fiddle
到目前为止描述的内容可能是一个简单的数据库查询。 'SELECT item2,count(*)FROM table WHERE item1 ='...'GROUP BY item2 ORDER BY COUNT(*)'。你有更多的数据吗?用户是否投票/评价项目? – sisve
没有额外的数据:(但可以有额外的列,它给item1后访问次数的次数没有使用简单的数据库查询的原因是,我想找到相似的用户,并相应地推荐item2(I只是想试试这个不知道它是否会工作)。 –