光标在红移上使用变量

发布于 2025-01-28 07:31:58 字数 718 浏览 3 评论 0原文

我有一个带有独特时间戳的桌子。

表名是T1,

scedtimestamp
2021-09-30 23:45:14 
2021-09-30 23:40:13 
2021-09-30 23:35:14 
2021-09-30 23:30:14 
2021-10-13 07:55:13 

我需要在红移上使用光标编写一个查询,以每5分钟比较每5分钟的时间戳。类似的内容:

select electricalbus,shadowprice from wm_lmp where scedtimestamp between '2021-09-30 23:45:14' and '2021-09-30 23:50:14'

上述代码中的所有日期都需要用时间戳和时间戳 + 5分钟替换,

begin;
declare cs cursor for
select distinct scedtimestamp as name from t1
fetch next from cs into temp;

select electricalbus,shadowprice from wm_lmp where scedtimestamp between temp and dateadd(m,5,temp)

close cs;
commit;

但是我的代码给出语法错误,说明了 - 错误:在“或接近”“进入”位置的语法错误。有人可以帮我吗?

I have a table with unique timestamps.

The table name is t1

scedtimestamp
2021-09-30 23:45:14 
2021-09-30 23:40:13 
2021-09-30 23:35:14 
2021-09-30 23:30:14 
2021-10-13 07:55:13 

I need to write a query using cursor on Redshift to compare those timestamps for every 5 mins. something like this :

select electricalbus,shadowprice from wm_lmp where scedtimestamp between '2021-09-30 23:45:14' and '2021-09-30 23:50:14'

all the dates in the above code need to be replaced with timestamp and timestamp + 5 mins

begin;
declare cs cursor for
select distinct scedtimestamp as name from t1
fetch next from cs into temp;

select electricalbus,shadowprice from wm_lmp where scedtimestamp between temp and dateadd(m,5,temp)

close cs;
commit;

but my code gives syntax errors says - ERROR: syntax error at or near "into" Position. Can someone help me out?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏雨凉 2025-02-04 07:31:58

似乎您希望循环浏览“选择不同”查询的结果。您不需要光标来执行此操作,只需要一个行变量和一个即可进行循环。这是一个可以帮助使用此操作的答案(对于不同的用例)。

将系统表或信息架构表与红移中的用户定义表一起加入

如果没有,您可以澄清您从存储过程中想要的结果。

It seems like you desire to loop on the results of the "select distinct" query. You don't need a cursor to do this, you just need a row variable and a for loop. Here's an answer that does this (for a different use case) that might help.

How to join System tables or Information Schema tables with User defined tables in Redshift

If not can you clarify what result you desire from the stored procedure.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文