Function Management

I am using Functions Management in Studio (javascript).

I am trying to run a query that will return all of my Downstream_Id associated to a particular Id. My script currently looks like this:

var db = [orient.getDatabase().query(“SELECT Id, Downstream_Id FROM Portfolio_Item WHERE Downstream_Id IS NOT NULL”)];

for (n = 0; n < db.length; n++) {
id = db[n];
return id;
}

This works fine, but the issue is that Downstream_Id has multiple values, separated by a comma (the data is being imported from a .csv file provided by another area).

So what I am getting from the query is:

 Id       |      Downstream_Id

 A       |           123, 456, 789
 B       |           234, 567
 C       |           345, 678

What I want is:

 Id       |      Downstream_Id

 A       |           123
 A       |           456
 A       |           789    
 B       |           234
 B       |           567
 C       |           345   
 C       |           678

The eventual goal here is to build an Edge relationship from the Id to all Downstream_Id. I am using the query function to try and get the results I am looking for, then the intent is to change the query to run a command to build the Edge.