title: Spark SQL grouping: Add to group by or wrap in first() if you don't care which value you get.;
body: <p>I have a query in Spark SQL like </p>
  
  <pre><code>select count(ts), truncToHour(ts)
  from myTable
  group by truncToHour(ts).
  </code></pre>
  
  <p>Where <code>ts</code> is of timestamp type, <code>truncToHour</code> is a UDF that truncates the timestamp to hour. This query does not work. If I try,</p>
  
  <pre><code>select count(ts), ts from myTable group by truncToHour(ts)
  </code></pre>
  
  <p>I got <code>expression 'ts' is neither present in the group by, nor is it an aggregate function. Add to group by or wrap in first() if you don't care which value you get.;</code>, but <code>first()</code> is not defined if I do:</p>
  
  <pre><code>select count(ts), first(ts) from myTable group by truncToHour(ts)
  </code></pre>
  
  <p>Anyway to get what I wanted without using a subquery? Also, why does it say "wrap in first()" but the <code>first()</code> is not defined?</p>