title: Find first OPENED_DATE for every Denied or Accepted date in Postgresql
body: <p>I need to find first document open date for request. For every request first step is opening a document and then it can be denied or accepted multiple times. I need to find first open_date between deny and accept dates. I tried <code>LEAD</code> function but it finds nearest one.
  My table looks like below:</p>
  <pre><code>id      |  document_id | oper_name           |    created_at      |
  --------------------------------------------------------------------
  24      |  102         | DOCUMENT_IS_OPENED  | 2020-07-06 11:40:18 |
  25      |  102         | DOCUMENT_IS_OPENED  | 2020-07-06 11:40:19 |
  27      |  102         | DOCUMENT_IS_OPENED  | 2020-07-06 11:40:27 |
  28      |  102         | DOCUMENT_IS_DENY    | 2020-07-06 11:40:31 |
  29      |  102         | DOCUMENT_IS_OPENED  | 2020-07-06 11:42:16 |
  30      |  102         | DOCUMENT_IS_OPENED  | 2020-07-06 11:45:01 |
  31      |  102         | DOCUMENT_IS_DENY    | 2020-07-06 11:48:30 |
  32      |  102         | DOCUMENT_IS_OPENED  | 2020-07-06 12:34:16 |
  33      |  102         | DOCUMENT_IS_OPENED  | 2020-07-06 13:12:01 |
  34      |  102         | DOCUMENT_IS_OPENED  | 2020-07-06 13:42:23 |
  35      |  102         | DOCUMENT_IS_OPENED  | 2020-07-06 14:40:23 |
  36      |  102         | DOCUMENT_IS_ACCEPTED| 2020-07-06 15:48:30 |
  37      |  102         | DOCUMENT_IS_OPENED  | 2020-07-06 16:20:45 |
  38      |  102         | DOCUMENT_IS_DENY    | 2020-07-06 16:41:30 |
  
  </code></pre>
  <p>My result should look like below:</p>
  <pre><code>id      |  document_id | oper_name           |    created_at       |  open_date           | parnt_id
  28      |  102         | DOCUMENT_IS_DENY    | 2020-07-06 11:40:31 |  2020-07-06 11:40:18 | 24
  31      |  102         | DOCUMENT_IS_DENY    | 2020-07-06 11:48:30 |  2020-07-06 11:42:16 | 29
  36      |  102         | DOCUMENT_IS_ACCEPTED| 2020-07-06 15:48:30 |  2020-07-06 12:34:16 | 32
  38      |  102         | DOCUMENT_IS_DENY    | 2020-07-06 16:41:30 |  2020-07-06 16:20:45 | 37
  </code></pre>
  <p>I tried below query but it does not work like I want</p>
  <pre><code>select * from (
  select id,document_id,event_type,created_at,
   LEAD(created_at,-1) OVER (ORDER BY created_at asc) as open_date
  from table where document_id = 102 order by created_at asc
  ) s where event_type in ('DOCUMENT_IS_DENY','DOCUMENT_IS_ACCEPTED')
  </code></pre>