Oh, please still be following this :-)....
Ok, what if I now also want to include from a vertex further up the travesal in each result? I tried a projection but it breaks the grouping logic.
So, something like:
g.V().as("aPerson").out("knows")
project("name", "agregates")
.by(select("aPerson").values("name"))
.by(
group().by("gender").by(
fold().match(__.as("p").count(local).as("total"),
__.as("p").unfold().values("age").mean().as("avg_age"),
__.as("p").order(local).by("age").as("o"),
__.as("o").limit(local, 1).values("name").as("youngest"),
__.as("o").tail(local, 1).values("name").as("oldest")
).select("total","avg_age","youngest","oldest")).next())
May not make sense with the modern data, but it's the pattern I'm trying to implement - a couple properties from "up the chain", combined with aggregates "down the chain" on each line.