如何在postgres中选择多个不同的值和最大值?

时间:2014-07-08 07:12:28

标签: sql postgresql

我有一个表记录特定rowid上的活动。样本数据是

rowid   activityid  status
323767  241288  Compromise
323767  227405  Untraced
323767  226356  Untraced
323767  226218  Reinvestigation ordered by court
323767  226217  Untraced
319859  225642  Reinvestigation ordered by court
319859  222568  Reinvestigation ordered by court
319807  222551  
319807  222524  
305458  204104  Reinvestigation ordered by court
305458  204002  Convicted

我需要为单个rowid获取activityid的最大值。如下所示

rowid   activityid  status
323767  241288  Compromise
319859  225642  Reinvestigation ordered by court
319807  222551  
305458  204104  Reinvestigation ordered by court

2 个答案:

答案 0 :(得分:2)

基于标准ANSI SQL的解决方案解决方案将使用共同相关的子查询

select t1.rowid, 
       t1.activityid,
       t1.status
from the_table t1
where t1.activityid = (select max(t2.activityid) 
                       from the_table t2
                       where t1.rowid = t2.rowid)
order by rowid;

或(更有效地)窗口函数:

select *
from (
  select rowid, 
         activityid,
         max(activityid) over (partition by rowid) as max_activity,
         status
  from the_table 
) t 
where activityid = max_activity
rowid;

但是在Postgres中,可以使用distinct on运算符

更有效地完成此操作
select distinct on (rowid) rowid, activityid, status
from the_table
order by rowid, activityid desc

distinct on的解决方案将是最快的,其次是使用窗口函数的解决方案,并且共同相关的子查询很可能是最慢的。

答案 1 :(得分:0)

你可以试试这个

select tableA.rowid,tableA.activityid,table.status
from  (select rowid,max(activityid)  as activityid 
        from table 
       group by rowid) tableA,table
where tableA.activityid = table.activityid