PostgreSQL:如何获取没有标记历史记录的所有记录

时间:2017-10-03 06:39:36

标签: postgresql

需要帮助...

我希望获取所有未标记特定备注的记录。

我拥有的是:

TABLE_1

` Records         Status`
  ABCDE01         Opened
  ABCDE02         Done
  ABCDE03         Transferred

TABLE_2

` Records         History_Status           Date` 
  ABCDE01         Received                 2017-09-01
  ABCDE01         Opened                   2017-09-02

  ABCDE02         Received                 2017-09-01
  ABCDE02         Opened                   2017-09-02
  ABCDE02         For Processing           2017-09-03 ----- MISSING DATA
  ABCDE02         Transferred              2017-09-03
  ABCDE02         Done                     2017-09-03

  ABCDE03         Received                 2017-09-01
  ABCDE03         Opened                   2017-09-02
  ABCDE03         For Processing           2017-09-03
  ABCDE03         Transferred              2017-09-03

我希望获得已经标记为已转移和已完成的所有记录但是没有#34;对于处理"历史状况。

表1捕获了最后一个状态。

表2将所有状态记录为其日志。

有人可以帮助我。

谢谢!

1 个答案:

答案 0 :(得分:1)

我没有对此进行测试,但它应该获取状态为Transferred但状态为For Processing的所有记录:

SELECT records
FROM table_1
   JOIN table_2 USING (records)
WHERE table_2.history_status = 'Transferred'
  AND NOT EXISTS (SELECT 1
                  FROM table_2 x
                  WHERE x.records = table_1.records
                    AND x.history_status = 'For Processing'
                 );