SQL for OrientDB - how to merge elements by ID and sum up attributes

I'm using SQL with Orient DB version 2.2. I want to merge elements of a class when they have the same ID and append the attribute of the one element to the attribute of the other one. This attribute is of type "embeddedset".

Let's assume I have 3 elements of the same class like this

+-------+------------+--------------+
| Elem  |     ID     | setAttribute |
+-------+------------+--------------+
|   1   |    id1     |    name1     |
|   2   |    id2     |    name1     |
|   3   |    id1     |    name2     |
+-------+------------+--------------+

I want to end up with this:

+-------+------------+--------------+
| Elem  |     ID     | setAttribute |
+-------+------------+--------------+
|   1   |    id1     | name1, name2 |
|   2   |    id2     |    name1     |
+-------+------------+--------------+

I tried as a start to find all the matches:

SELECT FROM (MATCH {class:elementClass,as:element1}, {class:elementClass, as:element2} RETURN element1, element2) WHERE (element1.id == element2.id AND [email protected] <> [email protected])

However, unfortunately I dont know how to continue? Thanks a lot!

728x90

1 Answers SQL for OrientDB - how to merge elements by ID and sum up attributes

You can try like below

select Elem, ID, $a.setAttribute from
(select  from t )
let $a=(select  from t where ID=$parent.current.ID)
group by ID

4 months ago