在 SQL 或 GQL 或 JDOQL 中,如何查询 2 列(差异最小)中具有最高值的行?
举例来说,我有一个表:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
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).
1) 您可能想使用其他一些指标来代替
abs(喜欢 - 不喜欢)
。在这种情况下,(0, 5)
和(100, 105)
将同样引起争议。我认为,
喜欢*不喜欢
可能适合您的情况。2)我不确定
jdoql
,但由于您指定了sql标签,在标准sql中这可以在不排序的情况下完成。像这样的东西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根据 Nikita 的建议,如果需要,您可以使用 (likes*dislikes) 作为 LikesTimesDislikes (用于指标)。
您甚至可以同时执行这两项操作:(
首先按总和,然后按指标)
As suggested by Nikita, you can use (likes*dislikes) as LikesTimesDislikes (for metrics) if you want.
You can even do both :
(First by sum, then by metrics)