如何从PostgreSQL中的两个列中获取具有最大值的记录

发布于 2025-02-13 12:32:23 字数 367 浏览 0 评论 0原文

我在postgressql中的表结构如下 则一个TicketID可以具有多个优先级和评级

如果我想要所有具有最高优先级为2和最大评分为3的机票ID的列表, 。 则输出应给出T2和T4。

然后,如果我想要所有具有最大优先级为3和最大额定值为3的机票ID的列表, 然后输出应给出T1,只有

我不确定如何编写此

”在此处输入图像说明”

I have table structure like below in postgressql
Where one ticketid can have multiple priority and rating

If i want the list of all ticket ids who have max priority as 2 and max rating as 3.
Then output should give t2 and t4

If i want the list of all ticket ids who have max priority as 3 and max rating as 3.
Then output should give t1 only

I am not sure how to write a query for this

enter image description here

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

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

发布评论

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

评论(1

人海汹涌 2025-02-20 12:32:23

您可以使用具有关键字来分组后过滤汇总:

SELECT ticketid 
FROM ratings 
GROUP BY ticketid 
HAVING max(priority) = 2 
AND max(rating) = 3;
 ticketid
----------
 t4
 t2
(2 rows)

You can use the HAVING keyword to filter an aggregate after grouping:

SELECT ticketid 
FROM ratings 
GROUP BY ticketid 
HAVING max(priority) = 2 
AND max(rating) = 3;
 ticketid
----------
 t4
 t2
(2 rows)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文