title: PostgreSQL query works in pgAdmin but not in Java EclipseLink
body: <p>I have a SQL that counts rows per date truncate (months, days, hours). Think a history graph. Query works fine if executed in pgAdmin but fails in Java using EclipseLink.</p>
  
  <p>pgAdmin query:</p>
  
  <pre><code>SELECT date_trunc( 'hour', delivered_at ),
         COUNT(date_trunc( 'hour', delivered_at )) AS num 
  FROM messages 
  WHERE channel_type='EMAIL' 
    AND created_at&gt;='2016-02-28 16:01:08.882' 
    AND created_at&lt;='2016-02-29 16:01:08.882' 
  GROUP BY date_trunc( 'hour', delivered_at );
  </code></pre>
  
  <p>JPQL Named query</p>
  
  <pre><code>SELECT FUNCTION('date_trunc', 'hour', m.deliveredAt ),
         COUNT(FUNCTION('date_trunc', 'hour', m.deliveredAt )) AS num 
   FROM Message m 
   WHERE m.channelType = :channelType 
     AND m.createdAt &gt;= :fromDate 
     AND m.createdAt &lt;= :toDate 
   GROUP BY FUNCTION('date_trunc', 'hour', m.deliveredAt )
  </code></pre>
  
  <p>EclipseLink debugging log:</p>
  
  <pre><code>SELECT date_trunc(?, delivered_at), COUNT(date_trunc(?, delivered_at)) FROM messages 
  WHERE (((channel_type = ?) AND (created_at &gt;= ?)) AND (created_at &lt;= ?)) GROUP BY date_trunc(?, delivered_at)
  bind =&gt; [hour, hour, EMAIL, 2015-12-27 00:00:00.0, 2015-12-27 00:00:00.0, hour]
  </code></pre>
  
  <p>Error:</p>
  
  <blockquote>
    <p>ERROR: column "messages.delivered_at" must appear in the GROUP BY
    clause or be used in an aggregate function   Position: 23</p>
  </blockquote>
  
  <p>PostgreSQL log:</p>
  
  <blockquote>
    <p>2016-03-01 13:22:08 CET ERROR:  column "messages.delivered_at" must
    appear in the GROUP BY clause or be used in an aggregate function at
    character 23 2016-03-01 13:22:08 CET STATEMENT:  SELECT date_trunc($1,
    delivered_at), COUNT(delivered_at) FROM messages WHERE (((channel_type
    = $2) AND (created_at >= $3)) AND (created_at &lt;= $4)) GROUP BY date_trunc($5, delivered_at) 2016-03-01 13:22:08 CET LOG:  execute
    S_2: SELECT 1</p>
  </blockquote>
  
  <p>If I replace the binded variables from EclipseLink logged query and execute it in pgAdmin the query works. What is going on here?</p>
  
  <p>Edit: Just to clarify, it also works using em.createNativeQuery.</p>