JuliaDB.jl icon indicating copy to clipboard operation
JuliaDB.jl copied to clipboard

How to perform `groupreduce` on multiple selected columns?

Open xiaodaigh opened this issue 7 years ago • 4 comments
trafficstars

I want to create the max(v1) and sum(v2) grouped by id.

This is my code

groupreduce(max, mytable, :id, select = :v1)
groupreduce(+, mytable, :id, select = :v2)

but given I am grouping by the same columnn, I would actually want to have a syntax like this to apply this at once

groupreduce((max,) , (+,), mytable, :id, select = (:v1, :v2))

xiaodaigh avatar Feb 25 '18 00:02 xiaodaigh

May be this is one of those cases where the JuliaDB syntax could be simplified a bit. You need something like:

func(i, j) = @NT(v1 = max(i.v1, j.v1), v2 = i.v2+j.v2)
groupreduce(func, mytable, :id, select = (:v1, :v2))

(you have to specify the reduction in terms of NamedTuples)

This should also work:

groupreduce((:max => :v1 => max, :sum => :v2 => +), mytable, :id, select = (:v1, :v2))

piever avatar Feb 25 '18 01:02 piever

groupreduce((:max => :v1 => max, :sum => :v2 => +), mytable, :id) is the simplest command which does this.

shashi avatar Feb 25 '18 06:02 shashi

Worth adding to the doc

xiaodaigh avatar Feb 25 '18 08:02 xiaodaigh

what is the syntax when 1) not using named tuples 2) using NDSparse and 3) no new column

maybe? groupreduce(( 1 => max, 2 => +), mytable, (1,2,3))

cwiese avatar May 06 '19 11:05 cwiese