title: Declare variables in redshift and use it in the select query
body: <p>I am trying to declare variables and use it in my sql query as shown below but it gives me error:</p>
  <pre><code>-- this will get the the timestamp in seconds
  DECLARE @startTimeStamp bigint = cast(DATEDIFF(s, '1970-01-01 00:00:00.000', '2016-12-09 16:22:17.897' ) as bigint)
  DECLARE @endTimeStamp bigint = cast(DATEDIFF(s, '1970-01-01 00:00:00.000', '2016-12-10 16:22:17.897' ) as bigint)
  
  -- my query.
  SELECT processId,
  houroftheday,
  minuteofhour,
  listagg(clientId, ',') within group (order by minuteofhour) as clientIds,
  count(*) as psg
  FROM data.process
  where kite = 'BULLS'
  and code is null
  and timestampinepochsecond &gt;= @startTimeStamp  AND  timestampinepochsecond &lt; @endTimeStamp
  group by 1, 2, 3
  </code></pre>
  <p>I read it <a href="https://stackoverflow.com/questions/30822176/declare-a-variable-in-redshift/40728524">here</a> as it isn't possible so how can I rewrite my above query so that it can work fine? I tried their example but having issues in converting it out.</p>
  <p>Below is what I have tried but I get syntax error:</p>
  <pre><code>CREATE TEMP TABLE tmp_variables AS SELECT 
     cast(DATEDIFF(s, '1970-01-01 00:00:00.000', '2016-12-09 16:22:17.897' ) as bigint) AS StartDate, 
     cast(DATEDIFF(s, '1970-01-01 00:00:00.000', '2016-12-10 16:22:17.897' ) as bigint)      AS EndDate;
  </code></pre>
  <p>Here is my <a href="https://dbfiddle.uk/?rdbms=postgres_9.6&amp;fiddle=a1755200dcbe2dc5c418b8a6314b0286" rel="nofollow noreferrer">demo</a> where it gives the error:</p>