从组中选择最大限制 1

发布于 2024-09-11 12:32:21 字数 929 浏览 3 评论 0原文

我正在制作一个网页缓存系统。我想制作一个简单的页面排名系统和输出。问题是,我想显示每个唯一域相关性得分最高的记录集。一个域可能有多个记录,但具有不同的标题、描述等。问题是,它不是获取包含唯一域的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

揽月 2024-09-18 12:32:21

我不确定为什么你的代码甚至可以工作,因为我认为你应该

ORDER BY Sum(relevance) DESC

而不是

ORDER BY relevance DESC

也许这就是问题所在?

除此之外,还有这个呢。它很丑陋,但它会起作用。如果 SQL Server 了解如何稍后在查询中引用别名,那就更好了。但唉。

SELECT title, html,
Case When title LIKE '%about%' Then 10 Else 0 End +
Case When html LIKE '%about%' Then 7 Else 0 End +
Case When keywords LIKE '%about%' Then 5 Else 0 End +
Case When description LIKE '%about%' Then 2 Else 0 End AS relevance
FROM page
WHERE Case When title LIKE '%about%' Then 10 Else 0 End +
Case When html LIKE '%about%' Then 7 Else 0 End +
Case When keywords LIKE '%about%' Then 5 Else 0 End +
Case When description LIKE '%about%' Then 2 Else 0 End > 0
ORDER BY Case When title LIKE '%about%' Then 10 Else 0 End +
Case When html LIKE '%about%' Then 7 Else 0 End +
Case When keywords LIKE '%about%' Then 5 Else 0 End +
Case When description LIKE '%about%' Then 2 Else 0 End DESC;

或者也许只是稍微重新安排一下:

SELECT title, html, relevance
FROM (SELECT title, html,
Case When title LIKE '%about%' Then 10 Else 0 End +
Case When html LIKE '%about%' Then 7 Else 0 End +
Case When keywords LIKE '%about%' Then 5 Else 0 End +
Case When description LIKE '%about%' Then 2 Else 0 End AS relevance
FROM page)
WHERE relevance > 0
ORDER BY relevance DESC;

I'm not sure why your code even works, since I think you should have

ORDER BY Sum(relevance) DESC

instead of

ORDER BY relevance DESC

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.

SELECT title, html,
Case When title LIKE '%about%' Then 10 Else 0 End +
Case When html LIKE '%about%' Then 7 Else 0 End +
Case When keywords LIKE '%about%' Then 5 Else 0 End +
Case When description LIKE '%about%' Then 2 Else 0 End AS relevance
FROM page
WHERE Case When title LIKE '%about%' Then 10 Else 0 End +
Case When html LIKE '%about%' Then 7 Else 0 End +
Case When keywords LIKE '%about%' Then 5 Else 0 End +
Case When description LIKE '%about%' Then 2 Else 0 End > 0
ORDER BY Case When title LIKE '%about%' Then 10 Else 0 End +
Case When html LIKE '%about%' Then 7 Else 0 End +
Case When keywords LIKE '%about%' Then 5 Else 0 End +
Case When description LIKE '%about%' Then 2 Else 0 End DESC;

Or maybe just a slight rearrangement:

SELECT title, html, relevance
FROM (SELECT title, html,
Case When title LIKE '%about%' Then 10 Else 0 End +
Case When html LIKE '%about%' Then 7 Else 0 End +
Case When keywords LIKE '%about%' Then 5 Else 0 End +
Case When description LIKE '%about%' Then 2 Else 0 End AS relevance
FROM page)
WHERE relevance > 0
ORDER BY relevance DESC;
墨离汐 2024-09-18 12:32:21

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文