简体   繁体   English

在 redshift 中声明变量并在 select 查询中使用它

[英]Declare variables in redshift and use it in the select query

I am trying to declare variables and use it in my sql query as shown below but it gives me error:我正在尝试声明变量并在我的 sql 查询中使用它,如下所示,但它给了我错误:

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

I read it here 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.我尝试了他们的示例,但在将其转换出来时遇到了问题。

Below is what I have tried but I get syntax error:以下是我尝试过的,但出现语法错误:

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;

Here is my demo where it gives the error:这是我的演示,它给出了错误:

So Redshift is not exactly Postgres and Postgres does not have DATEDIFF so your dbfiddle example doesn't help.所以 Redshift 不完全是 Postgres 并且 Postgres 没有 DATEDIFF 所以你的 dbfiddle 例子没有帮助。 I ran the code you provided on Redshift and got the following error message:我运行了您在 Redshift 上提供的代码并收到以下错误消息:

[Amazon](500310) Invalid operation: function pg_catalog.date_diff(character varying, character varying, character varying) does not exist;

This basically lays out the issue '1970-01-01 00:00:00.000' is not a time stamp, it's a text string.这基本上说明了问题“1970-01-01 00:00:00.000”不是时间戳,而是文本字符串。 This needs to be cast to timestamp for use by DATEDIFF.这需要转换为时间戳以供 DATEDIFF 使用。

DATEDIFF returns a bigint so casting to bigint is not needed. DATEDIFF 返回一个 bigint,因此不需要转换为 bigint。

Also, there is already a builtin function to get the seconds since epoch (1/1/1970) - DATEPART().此外,已经有一个内置的 function 来获取自纪元 (1/1/1970) 以来的秒数 - DATEPART()。 Like this:像这样:

date_part(epoch, '2016-12-09 16:22:17.897'::timestamp)

Now DATEPART returns a DOUBLE data type so you may want to cast this it BIGINT if you don't want the sub-second information.现在 DATEPART 返回一个 DOUBLE 数据类型,因此如果您不想要亚秒级信息,您可能希望将其转换为 BIGINT。

If you like your style it will work like this:如果你喜欢你的风格,它会像这样工作:

CREATE TEMP TABLE tmp_variables AS SELECT 
   cast(DATEDIFF(s, '1970-01-01 00:00:00.000'::timestamp, '2016-12-09 16:22:17.897'::timestamp ) as bigint) AS StartDate, 
   cast(DATEDIFF(s, '1970-01-01 00:00:00.000'::timestamp, '2016-12-10 16:22:17.897'::timestamp ) as bigint)      AS EndDate,
   5556::BIGINT       AS some_id;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM