title: Redshift: Running query using GETDATE() at specified list of times
body: <p>So, I have a query that uses GETDATE() in <code>WHERE</code> and <code>HAVING</code> clauses:</p>
  
  <pre><code>SELECT GETDATE(), COUNT(*) FROM (
    SELECT 1 FROM events
    WHERE (event_time &gt; (GETDATE() - interval '25 hours'))
    GROUP BY id
    HAVING MAX(event_time) BETWEEN (GETDATE() - interval '25 hours') AND (GETDATE() - interval '24 hours')
  )
  </code></pre>
  
  <p>I'm basically trying to find the number of unique <code>id</code>s that have their latest <code>event_time</code> between 25 and 24 hours ago with respect to the current time.</p>
  
  <p><strong>The problem:</strong> I have another table <code>query_dts</code> which contains one column containing timestamps. Instead of running the above query on the current time, using <code>GETDATE()</code>, I need to run in on the timestamp of every entry of the <code>query_dts</code> table. Any ideas?</p>
  
  <p>Note: I'm not really storing <code>query_dts</code> anywhere. I've created it like this:</p>
  
  <pre><code>WITH query_dts AS (
    SELECT (
      DATEADD(hour,-(row_number() over (order by true)), getdate())
    ) as n
    FROM events LIMIT 48
  ),
  </code></pre>
  
  <p>which I got from <a href="https://www.periscopedata.com/blog/generate-series-in-redshift-and-mysql.html" rel="nofollow">here</a></p>