2

我正在使用 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
          )

任何可以帮助我的人,非常感谢!

4

4 回答 4

1

尝试这个:

select a.id,a.bpm_process_instance_id,a.actor_id 
from bpm_task_instance A 
inner join
    (select max(a.id) as id,a.bpm_process_instance_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
        )
group by a.bpm_process_instance_id)B
on A.bpm_process_instance_id=B.bpm_process_instance_id
and A.id=B.id
于 2012-07-31T09:26:13.357 回答
1
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 () 作为(临时)视图,并改为引用它。

于 2012-07-31T09:45:08.957 回答
0

@wildplasser

    SELECT dt.* FROM 
    (
       SELECT id,bpm_process_instance_id,actor_id 
       FROM bpm_task_instance WHERE bpm_process_instance_id in 
       (
           SELECT bpm_process_instance_id FROM incident_info 
           WHERE status = 12 and registrant = 23
       ) 
    ) as dt
    WHERE NOT EXISTS (
        SELECT *
        FROM bpm_task_instance nx
        WHERE nx.bpm_process_instance_id = dt.bpm_process_instance_id
        AND nx.id > dt.id
    )
   ORDER BY id asc
于 2012-07-31T10:11:23.900 回答
0

在大范围内,DISTINCT ON语法有时比已经给出的完全有效的答案更快。

SELECT DISTINCT ON (instance_id)
  id, instance_id, actor_id
  FROM the_table dt
  ORDER BY instance_id, id DESC;

一旦习惯了这种语法,您可能会发现它比其他语法更容易阅读。在DISTINCT ON子句的括号内,您放置应该唯一的列列表,并且ORDER BY子句必须以匹配的列开头并继续使用足够的列以确保您要保留的列排在第一位。

于 2012-07-31T12:48:07.067 回答