我的表结构是(id,cluster,qid,priority)。我试图找出如何显示每个群集的最大优先级值。假设群集1的优先级为100,102,105。我想显示包含105的记录。请帮助。
答案 0 :(得分:3)
这篇文章解释了如何为每个组选择具有最大值的行。
http://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql/
答案 1 :(得分:2)
select cluster, MAX(priority) from structure group by cluster;
查找所有列TRY
select * from structure
where priority = (
select MAX(priority) from structure as s
where s.cluster = structure.cluster
);
答案 2 :(得分:2)
您可以使用内部联接筛选出行,例如:
select s.*
from structure s
join (
select cluster, MAX(priority) maxprio
from structure
group by
cluster
) filter
on s.cluster = filter.cluster
and s.priority = filter.maxprio
如果它们都具有该群集的最大优先级,则会返回多行。