Reputation: 107
I am using Gremlin to query Neptune.
I have 2 counts
How can I write a single query to get the numbers that result 1 divided by result 2? i.e. 'a': 0.5, 'b': 1
Upvotes: 0
Views: 236
Reputation: 10904
I can think of a few ways, but I guess using match()
is the easiest:
g.V().hasLabel(*).
union(count(),
out().groupCount().by('name')).fold().
match(__.as('values').limit(local, 1).as('c'),
__.as('values').tail(local, 1).unfold().as('kv'),
__.as('kv').select(values).math('_/c').as('v')).
group().
by(select('kv').by(keys)).
by(select('v'))
A similar query on the modern graph:
gremlin> g = TinkerFactory.createModern().traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> g.V().union(count(),
......1> out().groupCount().by(label)).fold().
......2> match(__.as('values').limit(local, 1).as('c'),
......3> __.as('values').tail(local, 1).unfold().as('kv'),
......4> __.as('kv').select(values).math('_/c').as('v')).
......5> group().
......6> by(select('kv').by(keys)).
......7> by(select('v'))
==>[software:0.6666666666666666,person:0.3333333333333333]
The next one is probably harder to understand, but would be my personal favorite (because a) I don't like match()
and b) it doesn't rely on the order of the results returned by union()
):
gremlin> g.V().
......1> groupCount('a').
......2> by(constant('c')).
......3> out().
......4> groupCount('b').
......5> by(label).
......6> cap('a','b').as('x').
......7> select('a').select('c').as('c').
......8> select('x').select('b').unfold().
......9> group().
.....10> by(keys).
.....11> by(select(values).math('_/c'))
==>[software:0.6666666666666666,person:0.3333333333333333]
Upvotes: 2