title: Conditional lead/lag function PostgreSQL?
body: <p>I have a table like this:</p>
  
  <pre><code>Name   activity  time
  
  user1  A1        12:00
  user1  E3        12:01
  user1  A2        12:02
  user2  A1        10:05
  user2  A2        10:06
  user2  A3        10:07
  user2  M6        10:07
  user2  B1        10:08
  user3  A1        14:15
  user3  B2        14:20
  user3  D1        14:25
  user3  D2        14:30
  </code></pre>
  
  <p>Now, I need a result like this:</p>
  
  <pre><code>Name   activity  next_activity
  
  user1  A2        NULL
  user2  A3        B1
  user3  A1        B2
  </code></pre>
  
  <p>I would like to check for every user the last activity from group A and what type of activity took place next from group B (activity from group B always takes place after activity from group A). Other types of activity are not interesting for me. I've tried to use the <code>lead()</code> function, but it hasn't worked.</p>
  
  <p>How I can solve my problem?</p>