title: PostgreSQL, GROUP BY and Window function
body: <p>I've got the following query in <code>PostgreSQL</code> which works showing records:</p>
  
  <pre><code>'SELECT TO_CHAR(date_trunc(\'hour\', time::timestamp), \'YYYY-MM-DD HH24:MI:SS\') AS time_from,
          TO_CHAR(date_trunc(\'hour\', time::timestamp) + interval \'1 hour\', \'YYYY-MM-DD HH24:MI:SS\') AS time_to,
          SUM(km) AS km
   FROM cars 
   GROUP BY date_trunc(\'hour\', time::timestamp) LIMIT 1000 OFFSET 50'
  </code></pre>
  
  <p>I would like to add <code>SUM(km) OVER() as km_total</code> for getting the total amount of <code>km</code> as I have <code>limit</code> and <code>offset</code> in my query.</p>
  
  <p>But then I get the error:</p>
  
  <pre><code>error: column "cars.km" must appear in the GROUP BY clause or be used in an aggregate function
  </code></pre>
  
  <p>But I'm not allowed to add it to my <code>GROUP BY</code>, this gives me the following error:</p>
  
  <pre><code>error: window functions are not allowed in GROUP BY
  </code></pre>