2015-04-20 39 views
0

我在下面的格式有一個csv文件/表格數據,建議基於項目歷史

UserId Item1 Item2     
1  url1 url3                      
1  url4 url6    
2  url2 url3   
2  url2 url4  
2  url4 url6  
3  url4 url6  
3  url2 url3  

所以,在這裏我想如果ITEM1的值是已知的預測ITEM2的perticular用戶。我們可以使用相同的協作過濾嗎?如果是的話,請引導:)

+0

到目前爲止描述的內容可能是一個簡單的數據庫查詢。 'SELECT item2,count(*)FROM table WHERE item1 ='...'GROUP BY item2 ORDER BY COUNT(*)'。你有更多的數據嗎?用戶是否投票/評價項目? – sisve

+0

沒有額外的數據:(但可以有額外的列,它給item1後訪問次數的次數沒有使用簡單的數據庫查詢的原因是,我想找到相似的用戶,並相應地推薦item2(I只是想試試這個不知道它是否會工作)。 –

回答

0

我相信它會工作,你只需要弄清楚你是如何決定用戶是否相似。以下給出了一個基於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