我正在使用 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
)
任何可以帮助我的人,非常感谢!