title: PostgreSQL get latest rows/events for all users
body: <p>Working on PostgreSQL 8.x (AWS Redshift)</p>
  
  <p>I have a db structure like this:</p>
  
  <pre><code>userId: varchar, tstamp: datetime, event: string
  </code></pre>
  
  <p>So let's say I have the following rows</p>
  
  <pre><code>u2, t1, e1
  
  u1, t2, e2
  
  u2, t3, e1
  
  u1, t4, e2
  </code></pre>
  
  <p>Where u1 and u2 are userids, t[1..4] are timestamps where t1>t2>t3>t4
  and e1 and e2 are types of events.</p>
  
  <p>So how do I get latest events performed by all users. So the output of the query would be:</p>
  
  <pre><code>u2, t3, e1
  
  u1, t4, e2
  </code></pre>
  
  <p>Tried to understand using:
  <a href="https://en.wikipedia.org/wiki/Correlated_subquery" rel="noreferrer">https://en.wikipedia.org/wiki/Correlated_subquery</a>
  and
  <a href="https://stackoverflow.com/questions/9472731/postgresql-selecting-most-recent-entry-for-a-given-id">PostgreSQL Selecting Most Recent Entry for a Given ID</a></p>
  
  <p>But I guess m a slow brain. Couldn't get it.</p>