I have something like that, i need to get "triangular" number – created by summing all natural ones from 0 to n.
I have such code, what should i write so i could
SUM name_of_column FROM generate_series?
SELECT n,
CASE
WHEN n > 0 THEN
SUM value FROM generate_series(1,n)
ELSE 0
END
AS res
FROM triangular
I tried to find "adecvate" PostgreSQL documentation wikilike to see returning table "attributes" of generate_sequence "class"
2
Answers
Are you searching for the cumulative
SUM()
window function?demo:db<>fiddle
or a simple aggregation?
demo:db<>fiddle
If you don’t supply an alias, the name is just "generate_series". I don’t know where this is documented, but it is discoverable by using
*
and looking at the header:Or if you assign a column alias, then that is the name:
If you supply a table alias but not a column alias, then the column name is a copy of the table name.