Search code examples
orientdbgraph-databases

OrientDB select unique Vertices from multiple Edges


I have 2 vertices User and Stamp. Vertices are related by three edges Have, WishToHave and Selling.

I'm wish to select unique Stamps that have any relation with User. To do it I was running this command:

select expand(out('Have', 'WishToHave', 'Selling')) from #12:0

The problem with this command is that it returns 'Stamp1' few times, because it has Have and Selling edges.

How can I select all unique/distinct Stamps related to User1?

To init test data for this example:

create class User extends V
create class Stamp extends V

create class Have extends E
create class WishToHave extends E
create class Selling extends E

create vertex User set name = 'User1'
create vertex Stamp set name = 'Stamp1'
create vertex Stamp set name = 'Stamp2'
create vertex Stamp set name = 'Stamp3'


create edge Have from (select from User where name = 'User1') to (select from Stamp where name = 'Stamp1')
create edge WishToHave from (select from User where name = 'User1') to (select from Stamp where name = 'Stamp2')
create edge Selling from (select from User where name = 'User1') to (select from Stamp where name = 'Stamp1')
create edge Selling from (select from User where name = 'User1') to (select from Stamp where name = 'Stamp3')

Solution

  • I tried your case with your structure:

    enter image description here

    To retrieve unique vertices you could use the DISTINCT() function. I can give you two examples:

    Query 1: Using EXPAND() in the target query

    SELECT EXPAND(DISTINCT(@rid)) FROM (SELECT EXPAND(out('Have', 'WishToHave', 'Selling')) FROM #12:0)
    

    Output:

    enter image description here

    Query 2: Using UNWIND in the target query

    SELECT EXPAND(DISTINCT(out)) FROM (SELECT out('Have', 'WishToHave', 'Selling') FROM #12:0 UNWIND out)
    

    Output:

    enter image description here

    Hope it helps