用于查找已定义集中具有公共标记的用户的SQL查询

时间:2011-05-20 10:59:35

标签: sql

amydeshane  0.180751    games
amydeshane  0.178772    video

我需要一个查找这样的实例的查询,其中用户从一个集合中有多个标记。

例如where Category in ('games','video','flash')

我希望与该集合共有的标签越多,它们的排名就越高。例如'amydeshane'应该有matching_terms = 2

任何想法?

现在这是我的查询,但它没有给我我需要的结果,因为我希望'amydeshane'排名更高,因为它包含更多标签匹配

SELECT        TOP (10) Username, tfidf AS TotalUsed
FROM            UserInfo
WHERE        (Category IN ('video', 'graphics', 'editor', 'games', 'youtube'))
GROUP BY Username, tfidf
HAVING        (COUNT(Username) > 1)
ORDER BY TotalUsed DESC

结果如下:

kingjames23 0.626885
F_David 0.406635
bjhscomputers   0.401741
jaw6    0.347777
lkw5151604  0.257147
anniemalahus    0.242461
opusfluke   0.240047
pporto  0.235550
amydeshane  0.180751
amydeshane  0.178772

2 个答案:

答案 0 :(得分:3)

select username, sum(tfidf) as totalused
from userinfo
where category in(...)
group by username
having count(category) > 1
order by sum(tfidf) desc

答案 1 :(得分:1)

我猜测你的结果是每个其他用户只有1个标签?

我不确定这是不是你想要的,但是你可以对用户名字段和总使用字段的总和进行计算......

SELECT TOP (10) Username, COUNT(Username) AS TagCount, SUM(tfidf) AS TotalUsed 
FROM UserInfo 
WHERE (Category IN ('video', 'graphics', 'editor', 'games', 'youtube')) 
GROUP BY Username, tfidf 
HAVING (COUNT(Username) > 1) 
ORDER BY COUNT(Username),TotalUsed DESC