sql - 如何从postgresql中的sql结果中获得最大结果

标签 sql postgresql group-by distinct-on

我正在使用 postgresql 8.3,并且我有一个简单的 sql 查询:

SELECT a.id,a.bpm_process_instance_id,a.actor_id 
  FROM bpm_task_instance a
 WHERE a.bpm_process_instance_id IN 
(
   SELECT bpm_process_instance_id 
         FROM incident_info 
        WHERE status = 12
          AND registrant = 23
)

所以,我得到了这样的结果集:

id    instance_id  actor_id
150     53            24
147     53            26
148     53            25
161     57            26
160     57            26
158     57            24
165     58            23
166     58            24
167     58            24

现在,我想通过instance_id获取最大id,结果就像爆炸

id    instance_id  actor_id
150     53            24
161     57            26
167     58            23

我怎样才能得到结果?我使用以下sql,但出现错误。

错误:关系“x”不存在

SELECT * 
  FROM (SELECT a.id,a.bpm_process_instance_id,a.actor_id 
          FROM bpm_task_instance a
         WHERE a.bpm_process_instance_id IN
            (
               SELECT bpm_process_instance_id 
                     FROM incident_info
                        WHERE status = 12
                      AND registrant = 23
            ) 
     ) AS x
 WHERE x.id = (
       SELECT max(id)
             FROM x 
            WHERE bpm_process_instance_id = x.bpm_process_instance_id
          )

谁能帮助我,非常感谢!

最佳答案

DROP SCHEMA tmp CASCADE;
CREATE SCHEMA tmp ;
SET search_path=tmp;

CREATE TABLE the_table
        ( id INTEGER NOT NULL
        , instance_id INTEGER NOT NULL
        , actor_id INTEGER NOT NULL
        );
INSERT INTO the_table(id, instance_id, actor_id) VALUES
(150,53,24) ,(147,53,26) ,(148,53,25)
,(161,57,26) ,(160,57,26) ,(158,57,24)
,(165,58,23) ,(166,58,24) ,(167,58,24)
        ;

SELECT id, instance_id, actor_id
FROM the_table dt
WHERE NOT EXISTS (
        SELECT *
        FROM the_table nx
        WHERE nx.instance_id = dt.instance_id
        AND nx.id > dt.id
        );

结果(注意:最后一行不同!):

DROP SCHEMA
CREATE SCHEMA
SET
CREATE TABLE
INSERT 0 9
 id  | instance_id | actor_id 
-----+-------------+----------
 150 |          53 |       24
 161 |          57 |       26
 167 |          58 |       24
(3 rows)

更新:这是包含其他子查询和缺失表以及原始(丑陋)列名的查询,全部打包到 CTE 中:

WITH zcte AS (
        SELECT ti.id AS id
                , ti.bpm_process_instance_id AS instance_id
                , ti.actor_id AS actor_id
        FROM bpm_task_instance ti
        WHERE EXISTS ( SELECT * FROM incident_info ii
                WHERE ii.bpm_process_instance_id = ti.bpm_process_instance_id
                AND ii.status = 12
                AND ii.registrant = 23
                )
        )
SELECT id, instance_id, actor_id
FROM zcte dt
WHERE NOT EXISTS (
        SELECT *
        FROM zcte nx
        WHERE nx.instance_id = dt.instance_id
        AND nx.id > dt.id
        );

更新附录:

糟糕,坏消息是 8.3 还没有 CTE。 (考虑升级)。好消息是:作为一种解决方法,您可以将 zcte () 作为(临时) View ,并引用它。

关于sql - 如何从postgresql中的sql结果中获得最大结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11736849/

相关文章:

mysql - 如何从 'SELECT *' 语句中排除列?

mysql - SQL FOREIGN KEY CONSTRAINT命名差异

database - Postgresql:数据库不接受命令以避免环绕数据丢失

c# - 继承 Newtonsoft JSON.NET 的反序列化无法正常工作

postgresql - 如何评估复合键?

SQL Server : Only last entry in GROUP BY

sql - 根据条件求和sql中的列

sql - 比较来自两个不同表的两列的逗号分隔值

mysql - SQL 查询以检索各种 DATE 范围内的 SUM

python - 使用 Pandas 从groupby中挑选随机元素