2

我的表结构是(id,cluster,qid,priority)。我试图弄清楚如何显示每个集群的最大优先级值。假设集群 1 的优先级为 100、102、105。我想显示包含 105 的记录。请帮忙。

4

3 回答 3

3

这是一篇文章,解释了如何为每个组选择具有最大值的行。

http://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql/

于 2010-06-14T08:11:09.930 回答
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
              );
于 2010-06-14T08:11:27.963 回答
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

如果它们都具有该集群的最大优先级,这将返回多行。

于 2010-06-14T08:21:47.987 回答