The Vertica Forum recently got a makeover! Let us know what you think by filling out this short, anonymous survey.
Please take this survey to help us learn more about how you use third party tools. Your input is greatly appreciated!

Calculate row Sum.

edited January 2018 in General Discussion

Hi!

I wrote UDScalar Function on C++ if any one need to calculate sum of row.

Any feedback would be greatly appreciated!

Exmaple

Data:

daniel=> select * from eldj ;
 col  | m01 | m02 | m03 | m04 | m05
------+-----+-----+-----+-----+-----
 key1 |   0 |   0 |   1 |   0 |   0
 key2 |   1 |   1 |   0 |   1 |   1
 key3 |   0 |   0 |   0 |   0 |   1
 key4 |   0 |   0 |   1 |   1 |   1
 key5 |   1 |   1 |   0 |   0 |   0
 key6 |   0 |   1 |   0 |   0 |   0
(6 rows)

Usage:

daniel=> select col, rowsum(* using parameters exclude='col') as 'SUM_TRUE' from eldj;
 col  | SUM_TRUE
------+----------
 key1 |        1
 key2 |        4
 key3 |        1
 key4 |        3
 key5 |        2
 key6 |        1
(6 rows)

Comments

  • I would try to move the exclude check into the setup() function - compute the column indexes to ignore in the processBlock function. That way you avoid string comparison for every row/column.

  • edited January 2018

    Hi @Ben_Vandiver!

    avoid string comparison for every row/column.

    I wanted to do it, but if nobody needs it then I'm not going to invest in it.

    So far its just a concept, there are a couple things I need to do:

    • columns type validation
    • logging
    • extend function for other numeric types(may be)
    • Handling Cancel Requests (not sure, because its a scalar function and ^C should work fine in this case)

    Thnx for your feedback.

  • Jim_KnicelyJim_Knicely - Select Field - Administrator

    Thanks for sharing this @sKwa!

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file