从组中选择最大限制 1
我正在制作一个网页缓存系统。我想制作一个简单的页面排名系统和输出。问题是,我想显示每个唯一域相关性得分最高的记录集。一个域可能有多个记录,但具有不同的标题、描述等。问题是,它不是获取包含唯一域的 1 个记录集,而是对该唯一域的所有记录集进行分组并全部输出。我只想要每个组的每个唯一域具有最高相关性得分的记录集,然后再输出我得到的下一个(以及与该组具有最高相关性的不同域)
SELECT title, html, sum(relevance) FROM
(
SELECT title, html, 10 AS relevance FROM page WHERE title like ‘%about%’ UNION
SELECT title, html, 7 AS relevance FROM page WHERE html like ‘%about%’ UNION
SELECT title, html, 5 AS relevance FROM page WHERE keywords like ‘%about%’ UNION
SELECT title, html, 2 AS relevance FROM page WHERE description like ‘%about%’
) results
GROUP BY title, html
ORDER BY relevance desc;
:
domain1 title html
domain1 title html
domain1 title html
domain2 title html
domain2 title html
domain2 title html
我想要的是
domain1 title html
domain2 title html
domain3 title html
domain4 title html
domain5 title html
I'm making an in webpage cache system. I wanted to make a simple page rank system along with output. The problem is, I want to display the recordset with the highest relevance score per unique domain. One domain may have multiple records but with different titles, descriptions, etc. The problem is, instead of getting 1 recordset containing a unique domain, it groups all the recordsets of that unique domain and outputs them all. I just want the recordset with the highest relevance score per unique domain per group before it outputs the next (and different domain with the highest relevance for that group)
SELECT title, html, sum(relevance) FROM
(
SELECT title, html, 10 AS relevance FROM page WHERE title like ‘%about%’ UNION
SELECT title, html, 7 AS relevance FROM page WHERE html like ‘%about%’ UNION
SELECT title, html, 5 AS relevance FROM page WHERE keywords like ‘%about%’ UNION
SELECT title, html, 2 AS relevance FROM page WHERE description like ‘%about%’
) results
GROUP BY title, html
ORDER BY relevance desc;
I'm getting:
domain1 title html
domain1 title html
domain1 title html
domain2 title html
domain2 title html
domain2 title html
What I want is
domain1 title html
domain2 title html
domain3 title html
domain4 title html
domain5 title html
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定为什么你的代码甚至可以工作,因为我认为你应该
而不是
也许这就是问题所在?
除此之外,还有这个呢。它很丑陋,但它会起作用。如果 SQL Server 了解如何稍后在查询中引用别名,那就更好了。但唉。
或者也许只是稍微重新安排一下:
I'm not sure why your code even works, since I think you should have
instead of
Maybe that's the problem?
Beyond that, what about this. It is ugly, but it will work. It would be better if SQL Server understood how to refer to aliases later in the query. But alas.
Or maybe just a slight rearrangement:
ORDER BY 相关性导致您的查询的行为就像相关性(非聚合)位于 SELECT 子句中一样。埃里克是对的 - ORDER BY sum(relevance) 应该可以纠正你的错误。
ORDER BY relevance is causing your query to behave as though relevance (non-aggregated) is in the SELECT clause. Erick is right -- ORDER BY sum(relevance) should fix your mistake.