julianomontini
julianomontini

Reputation: 335

Gremlin - sum values in group

I've been trying for days to get the following result:

I have this dataset:

e1 = g.addV('company').property('name', 'company-1').next()
e2 = g.addV('company').property('name', 'company-2').next()
p1 = g.addV('product').property('name', 'product-1').next()
p2 = g.addV('product').property('name', 'product-2').next()
p3 = g.addV('product').property('name', 'product-3').next()
i1 = g.addV('tax').property('name', 'tax-1').next()
i2 = g.addV('tax').property('name', 'tax-2').next()
g.addE('taxes').from(i1).to(p1).property('amount', 10)
g.addE('taxes').from(i2).to(p2).property('amount', 15)
g.addE('taxes').from(i1).to(p3).property('amount', 20)
g.addE('taxes').from(i2).to(p3).property('amount', 20)
g.addE('sells').from(e1).to(p1).property('price', 10)
g.addE('sells').from(e1).to(p2).property('price', 15)
g.addE('sells').from(e2).to(p2).property('price', 20)
g.addE('sells').from(e2).to(p3).property('price', 25)

And I need to sum all the products that a company sells( company-1 = 25, company-2 = 45 ). Currently I'm stuck at the following query:

g.V().hasLabel('company').as('e').outE('sells').as('f').inV().as('p').inE('taxes').as('i').outV().select('e', 'p', 'f', 'i').by('name').by('name').by('price').by('amount').group().by('e')

Any tips on how I may accomplish this? Also, if someone could give me an example of how I could do the sum, but taking in account the taxes, like: value = product * (1 + (amount/100))

Upvotes: 2

Views: 666

Answers (1)

bechbd
bechbd

Reputation: 6341

I think this gives you what you need by first grouping by the company and then summing the prices on the edges:

g.V().hasLabel('company').as('e').
    outE('sells').as('f').
    group().
        by(select('e').values('name')).
        by(values('price').sum())

Upvotes: 3

Related Questions