在 SQL 或 GQL 或 JDOQL 中,如何查询 2 列(差异最小)中具有最高值的行?

发布于 2024-10-17 12:44:55 字数 285 浏览 8 评论 0原文

举例来说,我有一个表:

Table: Message
| data | likes | dislikes |

如何有效找到喜欢和不喜欢数量最多的行,即最具争议的行。因此,对于以下示例:

{("Hello", 10, 5)
("Ola!", 5, 5)
("Ni Hao!", 2, 2)
("Wazzup!", 5, 7)}

将选择“Hello”。

任何帮助将不胜感激!

Say for example I have a table:

Table: Message
| data | likes | dislikes |

How can I efficiently find the row that has the highest number of likes and dislikes i.e. most controversial. So for the following example:

{("Hello", 10, 5)
("Ola!", 5, 5)
("Ni Hao!", 2, 2)
("Wazzup!", 5, 7)}

"Hello" would be chosen.

Any help would be highly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

原野 2024-10-24 12:44:55

GQL(假设您使用的是应用程序引擎数据存储区)将无法对计算进行查询。

您需要向模型添加另一个属性(例如称为“争议”),并在每次更改喜欢或不喜欢的数量时计算它。然后,您可以对其他属性执行简单的查询(即按降序排列,然后获取前 N 条记录)。

GQL (assuming you're using the app engine datastore) won't be able to do a query on a calculation.

You would need to add another property to your model (eg called 'controversy'), and calculate it every time you change the number of likes or dislikes. Then you could do a simple query on that other property (ie in descending order, then fetch the first N records).

日久见人心 2024-10-24 12:44:55

1) 您可能想使用其他一些指标来代替 abs(喜欢 - 不喜欢)​​。在这种情况下,(0, 5)(100, 105) 将同样引起争议。
我认为,喜欢*不喜欢可能适合您的情况。

2)我不确定jdoql,但由于您指定了sql标签,在标准sql中这可以在不排序的情况下完成。像这样的东西

select * from Message 
    where likes*dislikes = (select max(likes*dislikes) from Message)

1) You might want to use some other metrics instead of abs(likes - dislikes). In this case, (0, 5) and (100, 105) will be equally controversial.
I think, likes*dislikes might work in your conditions.

2) I'm not sure about jdoql, but since you specified sql tag, in standard sql this can be done without sorting. Something like

select * from Message 
    where likes*dislikes = (select max(likes*dislikes) from Message)
走野 2024-10-24 12:44:55
select top 1 (likes+dislikes ) as sumOfLikesDislikes from Message 
order by sumOfLikesDislikes desc

根据 Nikita 的建议,如果需要,您可以使用 (likes*dislikes) 作为 LikesTimesDislikes (用于指标)。

您甚至可以同时执行这两项操作:(

select top 1 (likes+dislikes ) as sumOfLikesDislikes, 
             (likes*dislikes ) as LikesTimesDislikes  
from Message 
order by sumOfLikesDislikes desc, LikesTimesDislikes   desc

首先按总和,然后按指标)

select top 1 (likes+dislikes ) as sumOfLikesDislikes from Message 
order by sumOfLikesDislikes desc

As suggested by Nikita, you can use (likes*dislikes) as LikesTimesDislikes (for metrics) if you want.

You can even do both :

select top 1 (likes+dislikes ) as sumOfLikesDislikes, 
             (likes*dislikes ) as LikesTimesDislikes  
from Message 
order by sumOfLikesDislikes desc, LikesTimesDislikes   desc

(First by sum, then by metrics)

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