2

私はpostgresql8.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
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として作成し、代わりにそれを参照することができます。

于 2012-07-31T09:45:08.957 に答える
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 に答える
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 に答える
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 に答える