title: JPA: Convert SQL query to JPA query
body: <p>How do I convert the following query to JPA query:</p>
  
  <pre><code>  select * from (select * from t1 where id=14 order by timestamp desc) as h group by hnumber order by timestamp desc limit 0, 15
  </code></pre>
  
  <p>I have tried it doing it like below but failed with syntax error.</p>
  
  <pre><code>  select m from (select n from t1 n where n.id=:id order by timestamp desc) as m group by hnumber order by timestamp desc limit 0, 15
  </code></pre>
  
  <p>Please correct me, basically I need to fetch distinct records in descending order of timestamp. Any help is appreciated.</p>
  
  <p>Updates:
  the columns in the t1 table are:</p>
  
  <pre><code>  seq
    id
    message
    timestamp
    hnumber
  </code></pre>
  
  <p>Basically I need to get distinct rows for a id (id is like user) and hnumber (can be multiple) in the descending order of timestamp. Timestamp should be greater with in the id and hnumber. </p>
  
  <p>Please note: the order of inserting to t1 table may not be in the order of timestamp. Each id can have different messages.</p>
  
  <p>Update 2:</p>
  
  <p>Here is an example:</p>
  
  <pre><code>    Seq     id  message timestamp       hnumber
      1001    14  xyz1    1394607463000   0429c3866c19981fc276855ff3cdaf100e0c9fdb
      1002    14  xyz2    1394608378000   0429c3866c19981fc276855ff3cdaf100e0c9fdb
      1003    14  xyz1    1394453678000   0429c3866c19981fc276855ff3cdaf100e0c9fdb
      1004    14  xyz2    1394608520000   0429c3866c19981fc276855ff3cdaf100e0c9fdb
      1005    14  xyz9    1394612791000   369d7cf7bd90fac78ef635b188e2a9952d77a8d1
      1006    14  xyz7    1394608513793   0429c3866c19981fc276855ff3cdaf100e0c9fdb
      1008    14  xyz6    1394608513793   0429c3866c19981fc276855ff3cdaf100e0c9fdb
      1009    14  xyz3    1394622221000   369d7cf7bd90fac78ef635b188e2a9952d77a8d1
      1010    14  xyz4    1394608513793   369d7cf7bd90fac78ef635b188e2a9952d77a8d1
  </code></pre>
  
  <p>Output I am expecting:</p>
  
  <pre><code>    Seq     id  message timestamp       hnumber
      1009    14  xyz3    1394622221000   369d7cf7bd90fac78ef635b188e2a9952d77a8d1
      1004    14  xyz2    1394608520000   0429c3866c19981fc276855ff3cdaf100e0c9fdb
  </code></pre>