title: SQL : Skipping Record of table (Java / JPA / Criteria Implementation)
body: <p>We have a table with historical data : id, department, totalInsert, totalUpdates, createdTime</p>
  
  <p>in which data is inserted every 1 hour.</p>
  
  <p><a href="https://i.stack.imgur.com/xAPDE.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xAPDE.jpg" alt="enter image description here"></a></p>
  
  <p>based on above data we need to fetch data as per provided duration and interval.</p>
  
  <p><strong>For Example :</strong> 
  Duration = 24 hours and Interval = 1 hour
  then last 24 hours data should be fetched for every 1 hour interval. (24 records)</p>
  
  <p>Duration = 24 hours and Interval = 4 hour
  then last 24 hours data should be fetched for every 4 hour interval. (6 records)</p>
  
  <p>Duration = 7 days and Interval = 1 day
  then 7 days data should be fetched for every 1 entry per day. (7 records)</p>
  
  <p>Duration = 21 days and Interval = 7 day
  then 21 days data should be fetched for every 1 entry per week. (3 records)</p>
  
  <p><a href="https://i.stack.imgur.com/pmPNn.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pmPNn.jpg" alt="enter image description here"></a></p>
  
  <p>So, can you help to have generic query for all above possible scenarios.
  Don't want separate query for each input provided by user.</p>
  
  <p><strong>Tried So Far :</strong></p>
  
  <blockquote>
    <p>SELECT *  FROM department_details  WHERE id IN ( SELECT MAX(id) FROM
    department_details  WHERE department='Computer Technology' AND
    ((date(createdDate) > '2019-01-09' AND date(createdDate) &lt;=
    '2019-01-10') OR          (date(createdDate) > '2019-01-15' AND
    date(createdDate) &lt;= '2019-01-16') OR           (date(createdDate) >
    '2019-01-21' AND date(createdDate) &lt;= '2019-01-22') OR<br>
    (date(createdDate) > '2019-01-27' AND date(createdDate) &lt;=
    '2019-01-28') OR           (date(createdDate) > '2019-02-02' AND
    date(createdDate) &lt;= '2019-02-03'))                GROUP BY
    date(createdDate) ) ORDER BY createdDate;</p>
  </blockquote>
  
  <p>Looking for Generic JPA Criteria solution</p>