Gary,
Getting closer to to the solution what I like to achieve. Understand what your proposal do but I realize I need to explain more in detail what I want and why I have two datasets of cables (PULLED_TO and PULLED_FROM).
The cable nodes are loaded from a CSV file with the structure below (just a part of the structure):
LOAD CSV WITH HEADERS FROM 'file:///CableList.csv' AS line FIELDTERMINATOR ';'
MERGE (cable:Cables{nummer_ID:line.Nummer_ID})
ON CREATE SET cable.locatie_1 = line.Locatie_1,
cable.length = toInteger(line.Lengte),
cable.van_object = line.Van_object, //means cable.from_object
cable.naar_object = line.Naar_object, //means cable.to_Object
cable.connectedFrom = line.Connected_from,
cable.connectedTo = line.Connected_to,
cable.mainCable = line.`Main Cable`,
cable.pulled = line.Pulled,
cable.systemAbreviation = line.`System Abreviation`;
The relationships which I have made are below (Reminder, the properties cable.van_object and cable.naar_Object (sorry for the DUTCH) are two different properties) :
//07. PULLED_FROM equipment to cable v0.2
MATCH (area:Area)
WITH area
MATCH (cable:Cables)
WHERE area.objectId = cable.van_object
CREATE (area)-[p:PULLED_FROM]->(cable);
//08. PULLED_TO Cable to equipment
MATCH (area:Area)
WITH area
MATCH (cable:Cables)
WHERE area.objectId = cable.naar_object
CREATE (cable)-[p:PULLED_TO]->(area);
// 09 Define Local Cables in Cable Nodes V1.0
MATCH (equipmentFrom:Area)-[pf:PULLED_FROM]->(cable)-[pt:PULLED_TO]->(equipmentTo:Area)
WHERE equipmentFrom.fromLocation = equipmentTo.fromLocation
SET cable.cableType = "Local";
// 10. Define Main Cables in Cable Nodes
MATCH (equipmentFrom:Area)-[pf:PULLED_FROM]->(cable)-[pt:PULLED_TO]->(equipmentTo:Area)
WHERE equipmentFrom.fromLocation <> equipmentTo.fromLocation
SET cable.cableType = "Main";
Try to explain the query below what I like to achieve:
Call {
// The cables (Main or Local) which are pulled TO the area can be "Routed", "Not Routed" or
//"LOCAL" or be "PULLED" or "NOT PULLED". **I like to know how many cables (Main, Local)
//going to this area with what length and if the are "PULLED" or "NOT PULLED".** Will be
//calculated in the RETURN clause. So far so good. The challenge is, the area. The area (name "X")
//can be specified in this part of the UNION OR specified in the other part of the UNION.
UNWIND ['Main','Local'] as cableIdType
MATCH (areaDescription:AreaDescription)<-[:HAVE_AD]-(area:Area)<-[:PULLED_TO]-(cables:Cables{cableType:cableIdType})
WHERE cables.mainCable IN ['Routed', 'Not Routed', 'Local'] OR cables.pulled IN ['Pulled', 'NOT Pulled']
RETURN
area.fromLocation AS location,
cables.pulled AS cablePullType,
cableIdType AS cableType,
count(*) as countOfCables,
sum(cables.length) AS sumOfLengths
UNION
// As specified, in this part of the UNION the area (name "X") can be located here also. So for this
// area I like to know also the number of cables and their length.
UNWIND ['Main','Local'] as cableIdType
MATCH (areaDescription:AreaDescription)-[:BELONGS_TO]->(area:Area)-[:PULLED_FROM]->(cables:Cables{cableType:cableIdType})
WHERE cables.mainCable IN ['Routed', 'Not Routed', 'Local'] OR cables.pulled IN ['Pulled', 'NOT Pulled']
RETURN
area.fromLocation AS location,
cables.pulled AS cablePullType,
cableIdType AS cableType,
count(*) as countOfCables,
sum(cables.length) AS sumOfLengths
}
//Now I know for the area (name "X") of both sides the length and number of cables and I just summatrize them to get the complete overview
WITH location, cablePullType, cableType, sum(countOfCables) AS countOfCables, sum(sumOfLengths) AS sumOfLengths
UNWIND [
{location: location, cablePullType: cablePullType, cableType: cableType, result: countOfCables, prefix: 'count'},
{location: location, cablePullType: cablePullType, cableType: cableType, result: sumOfLengths, prefix: 'length'}] as data
RETURN data.location as location, data.prefix+"-"+data.cableType+"-"+data.cablePullType as variable, data.result as result
The output of the query is expected, as follows (just a part of it):
location variable result variable occurences :
1000.146 "count-Main-Pulled" 231 "count-Main-Pulled"
1000.146 "length-Main-Pulled" 10002 "length-Main-Pulled"
1000.152 "count-Main-Pulled" 284 "count-Main-Not Pulled"
1000.152 "length-Main-Pulled" 12257 "length-Main-Not Pulled"
1000.234 "count-Main-Pulled" 348 "count-Local-Pulled"
1000.234 "length-Main-Pulled" 12776 "length-Local-Pulled"
1000.149 "count-Main-Pulled" 502 "count-Local-Not Pulled"
1000.149 "length-Main-Pulled" 26043 "length-Local-Not Pulled"
1000.147 "count-Main-Pulled" 116
1000.147 "length-Main-Pulled" 5890
1000.263 "count-Main-Pulled" 337
1000.263 "length-Main-Pulled" 28630
1000.296 "count-Main-Pulled" 9
1000.296 "length-Main-Pulled" 241
1000.44 "count-Main-Pulled" 662
1000.44 "length-Main-Pulled" 35925
1000.202 "count-Main-Pulled" 538
1000.202 "length-Main-Pulled" 20951
1000.45 "count-Main-Pulled" 130
1000.45 "length-Main-Pulled" 6895
1000.165 "count-Main-Pulled" 226
1000.165 "length-Main-Pulled" 11283
1000.41 "count-Main-Pulled" 489
1000.41 "length-Main-Pulled" 24769
1000.191 "count-Main-Pulled" 256
1000.191 "length-Main-Pulled" 13327
1000.187 "count-Main-Pulled" 375
1000.187 "length-Main-Pulled" 13926
My main goal is to translate the output into the variables below and store them into the Node "AreaDescription":
NrMainPulledCables
LengthMainPulledCables
NrMainNotPulledCables
LengthNotMainPulledCables
NrLocalPulledCables
LengthLocalPulledCables
NrLocalNotPulledCables
LengthNotLocalPulledCables
The translation of the output to these variables is for me another challenge.
The possible solution can be:
- Create a temporary node;
- Store in the node the output data of the query ;
- Use apoc.when to translate the output into the right variable;
- Store them in the Node "AreaDescription";
- Remove temporary node.
Proposal:
MERGE (cableData:cableDataPerArea);
Above QUERY;
MATCH (cableData:cableDataPerArea)
WITH data, cableData
SET cableData.location = data.location,
cableData.variable = data.prefix+"-"+data.cableType+"-"+data.cablePullType,
cableData.result = data.result
In this way the Node cableDataPerArea is only loaded with the last data of data.
What I need to change in the query that the temporary node is loaded with all the data?
Is the proposed way, the correct way?
Query which I have used.
MERGE (cableData:cableDataPerArea);
Call {
UNWIND ['Main','Local'] as cableIdType
MATCH (areaDescription:AreaDescription)<-[:HAVE_AD]-(area:Area)<-[:PULLED_TO]-(cables:Cables{cableType:cableIdType})
WHERE cables.mainCable IN ['Routed', 'Not Routed', 'Local'] OR cables.pulled IN ['Pulled', 'NOT Pulled']
RETURN
area.fromLocation AS location,
cables.pulled AS cablePullType,
cableIdType AS cableType,
count(*) as countOfCables,
sum(cables.length) AS sumOfLengths
UNION
UNWIND ['Main','Local'] as cableIdType
MATCH (areaDescription:AreaDescription)-[:BELONGS_TO]->(area:Area)-[:PULLED_FROM]->(cables:Cables{cableType:cableIdType})
WHERE cables.mainCable IN ['Routed', 'Not Routed', 'Local'] OR cables.pulled IN ['Pulled', 'NOT Pulled']
RETURN
area.fromLocation AS location,
cables.pulled AS cablePullType,
cableIdType AS cableType,
count(*) as countOfCables,
sum(cables.length) AS sumOfLengths
}
WITH location, cablePullType, cableType, sum(countOfCables) AS countOfCables, sum(sumOfLengths) AS sumOfLengths
UNWIND [
{location: location, cablePullType: cablePullType, cableType: cableType, result: countOfCables, prefix: 'count'},
{location: location, cablePullType: cablePullType, cableType: cableType, result: sumOfLengths, prefix: 'length'}] as data
//RETURN data.location as location, data.prefix+"-"+data.cableType+"-"+data.cablePullType as variable, data.result as result
MATCH (cableData:cableDataPerArea)
WITH data, cableData
SET cableData.location = data.location,
cableData.variable = data.prefix+"-"+data.cableType+"-"+data.cablePullType,
cableData.result = data.result