'Cursors using variables on redshift
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?
Solution 1:[1]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Bill Weiner |
