title: Postgresql OVER and GROUP
body: <p>I have a problem with grouping</p>
  
  <p>I have a table(a lot more stuff in it, but not relevant) that looks something like:</p>
  
  <pre><code>id user
  0 1
  1 1
  2 1
  3 2
  4 2
  5 2
  6 1
  7 1
  </code></pre>
  
  <p>I'm trying to get the following value:</p>
  
  <pre><code>user start end
  1 0 2
  2 3 5
  1 6 7
  </code></pre>
  
  <p>Basicaly, i need the first and last occurance of user, while not messing with order.
  I know i need to use OVER (PARTITION BY ...), But i've never used it and not sure how to build this query.
  if i "partition by user", it ignores order. And if i "partition by id, user" it again returns wrong.</p>
  
  <p>Example of what i tried(doesn't even try to get what i need, but is a midpoint that shows me how to do it once i figure out "over" part):</p>
  
  <pre><code>SELECT user, count(user) over (partition by user):
  user count
  1 5
  1 5
  1 5
  2 3
  2 3
  2 3
  1 5
  1 5
  
  SELECT user, count(user) over (partition by id, user):
  user count
  1 1
  1 1
  1 1
  2 1
  2 1
  2 1
  1 1
  1 1
  </code></pre>