0

Sample Data

g.addV('result').
property('marks', 100).
property('subject', 'Maths').
property('student', A)

g.addV('result').
property('marks', 50).
property('subject', 'English').
property('student', A)

I have details of multiple students, and I want to fetch student name and subject for those students whose highest marks are less than 80.

I tried this but has filter doesn't work after group by

g.V().
hasLabel('result').
order().
by('marks').
group().
by('student').
by(limit(1)).
has('marks', lte(80)).
project('Student', 'Marks').
by('student').
by('marks')

Any alternative possible?

Note : I want to write a query that is supported in Cosmos DB Gremlin API

1 Answers1

0

You can filter the results inside of the group step. like this:

g.V().hasLabel('result').
  order().by('marks').
  group().
    by('student').
    by(has('marks', lte(80)).
      project('Subject', 'Marks').
        by('subject').by('marks').fold())

But I think it will be better to filter the results before:

g.V().hasLabel('result').
    has('marks', lte(80)).
    group().
    by('student').
    by(
      project('Subject', 'Marks').
        by('subject').by('marks').fold())

If you have a bigger query and you still need to filter after the group you can use select

g.V().hasLabel('result').
  order().by('marks').
  group().
    by('student').unfold().
    select(values).unfold().
  has('marks', lte(80)).
  project('Student', 'Marks').
    by('student').by('marks')

example: https://gremlify.com/d4zw5qg1hc9y

noam621
  • 2,766
  • 1
  • 16
  • 26
  • But I want to make sure that the filter is applied only on vertex fetch after limit(1) applied in group by like if A's marks are 100, B's marks are 50, I want to fetch these 2 vetex, then apply the filter only on these. – Kajal Patel Jun 15 '20 at 12:43
  • @KajalPatel ok maybe I didn't understand the output you want. what should be the output? – noam621 Jun 15 '20 at 12:47
  • My actual scenario is much complex to explain right now, but in the context of above mentioned example, I want to project Student Name, Highest Marks, now here, I'll order by marks and group by student and by limit(1), so this will give me student and their highest marks, after that I want to apply a filter on that result. I hope that makes sense. – Kajal Patel Jun 15 '20 at 12:51
  • When I try this code, g.V().hasLabel('result'). order().by('marks'). group(). by('student'). by(has('marks', lte(80)). project('Subject', 'Marks'). by('subject').by('marks').fold()), __Column reference R_11[\"marks\"] cannot be located in the raw records in the current execution pipeline__ error is thrown – Kajal Patel Jun 15 '20 at 12:55
  • Is there an alternative for HAVING clause in gremlin? – Kajal Patel Jun 15 '20 at 13:13