Sql 如何使用时间戳列获取两个日期之间的数据?

Sql 如何使用时间戳列获取两个日期之间的数据?,sql,amazon-redshift,Sql,Amazon Redshift,我有一个下面的查询,它提供了过去X天的数据。相反,我想在两个日期之间获取数据,如果可能的话,这些数据应该是可配置的?有什么办法吗 SELECT processId, houroftheday, minuteofhour, listagg(clientId, ',') within group (order by minuteofhour) as clientIds, count(*) as psg FROM data.process where kite = 'BULLS' and

我有一个下面的查询,它提供了过去X天的数据。相反,我想在两个日期之间获取数据,如果可能的话,这些数据应该是可配置的?有什么办法吗

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 > date_part(epoch, sysdate) - X*24*60*60
 group by 1, 2, 3
timestampinepochsecond列具有此值-1599608655

如何使用timestampinepochsecond列获取两个日期之间的数据?如下所示:

 timestampinepochsecond between 2020-12-01 AND 2021-01-05
更新

我试图声明两个变量,并在我的查询中使用它们,如下所示,但它不起作用-

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,
   5556::BIGINT       AS some_id;

SELECT StartDate, EndDate FROM tmp_variables;

我认为您必须将日期时间转换为历元时间戳,然后根据该时间戳进行查询:

    -- 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)
    

-- then your 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 >= @startTimeStamp  AND  timestampinepochsecond 
 < @endTimeStamp
 group by 1, 2, 3

如果这是一个存储过程-您可以传入开始日期,结束日期是一个参数-然后转换为历元秒。

其中t.timestamp_col>='2020-12-01'和t.timestamp_col<'2021-01-05'我的timestampinepochsecond列的值如下-1599608655@MeghshyamSonarSorry我离开了一段时间。看起来我不能在这里使用Declare变量,在select查询中使用then。我也检查过了。有什么办法可以在红移中完成吗?我想在顶部声明startTimeStamp和endTimeStamp,就像您一样,并在下面的select查询中使用它们。这可能是偶然的吗?我在我的问题中更新了我已经尝试过的,但它给出了明显的语法错误-有没有想过我在那里做什么错了?