Vertica for loop to iterate over resultset
I want to loop over a set of rows and call user defined function to perform some operation on it
Say I have a table below, with hash value stored in the hash column of the table
Desc temp
————————————
id Number
name Varchar
hash VarBinary
Now I have a user defined function usr_fun which takes two hash values, first row hash value and next row hash value and performs some operation on them and then returns the new hash value as the result. Now it should use this result and pickup the third row hash value and execute the same funtion and continue like this for all the rows and finally return the result.
Select usr_fun(row.hash,nextrow.hash) as result from temp
Result
—————
XXXXXXXX
Same problem visually
select * from Temp
—————
Hash
—————
1
2
3
4
Select usr_fun(row.hash,nextrow.hash) as result from temp
(1+2=3, 3+3=6, 6+4=10)
Result
—————
10
Thanks
Comments
Hi jumanji
This seems so trivial to me that I assume I got something wrong.
But, from what you seem to be after,
The example below does all just in SQL - and it's not even Vertica specific
1. check the input values in the
input
Common Table expression in the WITH clause, including the hash value you're suggesting - of course, I have an ID to sort by later, as tables are never sorted.2. check
with_vertica_hash
, where I add a HASH() function on the first two columns of theinput
in-line table3. check the
SUM(jumanji_hash)
function call in the final SELECT, which behaves pretty much like you're expecting your function to behave.This is ANSI-1999 SQL. Not even Vertica specific. If I'm right, SQL seems to become more and more one of the best kept secrets as to its versatility and support for productiveness ...
Or did I get something wrong?
Happy playing - Marco
Hi Marcothesane,
Thanks Much!. On the similar lines in the actual problem. I have a SUM(jumanji_hash) which takes two arguments. I can't modify this function,so, I need to modify this query such that SUM(jumanji_hash,result) and finally return 10.
The function what I have only works on the two arguments that I pass and doesn't have access to rowset implicitly.
To put it in simpler words. how can we achieve the same output with
int user_sum(jumanji_hash,result) { return jumanji_hash+result;} it doesn't have access to the rows or any other informartion.