Two columns in a csv containing same data and i want relationship just to be created if both columns have different data. How to do that?

This is sample data in csv file.(Main column has all same values and i need to create a relation between Main and ColumnA and on clicking columnA nodes the nodes connected to that should be displayed)
|ColumnA|Main|ColumnB|
|TDWM.TDWMLOCK|TDWMObjectAssnSP|TDWM.TDWMLOCK|
|TDWM.TDWMABC|TDWMObjectAssnSP|TDWM.TDWMABC|
|TDWM.RuleDefs|TDWMObjectAssnSP|TDWM.RuleDefs|
|TDWM.RuleQualifyCriteria|TDWMObjectAssnSP|TDWM.RuleQualifyCriteria|
|SQLJ.JARS|TDWMObjectAssnSP|SQLJ.JARS|
|TDWM.RuleQualifyCriteria|TDWMObjectAssnSP|TDWM.RuleQualifyCriteria|
|TDWM.RuleQualifyCriteria|TDWMObjectAssnSP|TDWM.RuleQualifyCriteria|
|SQLJ.JARS|TDWMObjectAssnSP|DBC.DBASE|
|SQLJ.JARS|TDWMObjectAssnSP|DBC.DB1|
|SQLJ.JARS|TDWMObjectAssnSP|DBC.ABC|
|DBC.ABC|TDWMObjectAssnSP|DBC.Data1|
|DBC.ABC|TDWMObjectAssnSP|DBC.Data2|
|DBC.ABC|TDWMObjectAssnSP|DBC.MNOP|
|DBC.ABC|TDWMObjectAssnSP|DBC.XYZ1|
|DBC.ABC|TDWMObjectAssnSP|DBC.XYZ2|
|DBC.ABC|TDWMObjectAssnSP|DBC.XYZ3|
|DBC.DB1|TDWMObjectAssnSP|DBC.SAP|

here i dnt want the relationships between the same values in columnA and columnB.
please let me know if you have any solutions for this.
Thanks.

here is a quick one

LOAD CSV WITH HEADERS FROM "file:///neo_data.csv" AS row FIELDTERMINATOR ';'
MERGE(n:Main{fullname:row.Main})
MERGE(c1:Column{name:row.ColumnA})
MERGE(c2:Column{name:row.ColumnB})
CREATE UNIQUE (n)-[:has_connection]->(c1)
CREATE UNIQUE (n)-[:has_connection]->(c2)

query

match (n:Main)-[]-(m:Column) return n.fullname,m.name order by m.name
1 Like

Hi Dominic,
I have also done the same thing but my requirement is different.
for example in the first column we have SQLJ.JARS and also third column it has data DBC.DB1 and also DBC.DB1 is in first column which has other data so i need to create a relation as such so that when clicking on SQLJ.JARS it shows relationship with all its data and DBC.DB1 which comes in first column shows relationship when we click on it in SQLJ.JARS relation itself.

Thanks.

Try this:

I copied your data and created a .csv file to import.

Cypher Query:

USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM 'file:/manish.csv' AS row
FIELDTERMINATOR '|'
WITH row

FOREACH(ignoreMe IN CASE WHEN row.ColumnA <> row.ColumnB THEN [1] ELSE END |

MERGE(n:Main{fullname:row.Main})
MERGE(c1:Column{name:row.ColumnA})
MERGE(c2:Column{name:row.ColumnB})

MERGE (n)-[:has_connection]->(c1)
MERGE (n)-[:has_connection]->(c2)

)
;

In the FOREACH statement make sure you have two square (opening and closing) brackets after ELSE.

Added 12 labels, created 12 nodes, set 12 properties, created 11 relationships, completed after 332 ms.

Result:

manish

1 Like

Thanks a lot.
I got the idea , will try it if i face any issues will let you know :)

Note that you only need this FOREACH trick if you have additional operations to perform after this.

Otherwise you can simply use a WHERE clause so that you only apply the rest of the query when your condition is true:

USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM 'file:/manish.csv' AS row
FIELDTERMINATOR '|'
WITH row
WHERE row.ColumnA <> row.ColumnB

MERGE(n:Main{fullname:row.Main})
MERGE(c1:Column{name:row.ColumnA})
MERGE(c2:Column{name:row.ColumnB})

MERGE (n)-[:has_connection]->(c1)
MERGE (n)-[:has_connection]->(c2)
1 Like

and what if a value from columnA has a relationship with columnB and the value in ColumnB is present in coulumnA with relation to other value.
How to create that relation?

columnA Main columnB
A R B
F R C
C R T

so i need a graph of type:
REL

How to do this? I am getting everything except this.

I'm a little confused why you would use three values/nodes to create relationships (all outgoing from the "main" node) when you want a graph like this.

Couldn't you just use two columns, representing pair of nodes that need a relationship between them? Then it's just a MATCH a MATCH b MERGE relationship between them.

columnA  columnB
R              A
A              B
R              F
C              T
USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM 'file:/manish.csv' AS row
FIELDTERMINATOR '|'
WITH row
WHERE row.ColumnA <> row.ColumnB

MERGE(c1:Column{name:row.ColumnA})
MERGE(c2:Column{name:row.ColumnB})

MERGE (c1)-[:has_connection]->(c2)

If I've misunderstood what you're trying to do, please clarify what it is you want.

I want Main to be the main node and upon clicking on main it will show all relations with ColumnA and upon clicking values of columnA it will give relation with columnB.

In that case, if you intend to keep your CSV in the same format, you would change your merges such that column b is connected to column a. Previous queries were connecting both to the main node.

USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM 'file:/manish.csv' AS row
FIELDTERMINATOR '|'
WITH row
WHERE row.ColumnA <> row.ColumnB

MERGE(n:Main{fullname:row.Main})
MERGE(c1:Column{name:row.ColumnA})
MERGE(c2:Column{name:row.ColumnB})

MERGE (n)-[:has_connection]->(c1)
MERGE (c1)-[:has_connection]->(c2)

And if the direction of the relationship doesn't matter to you, and you just need a connection present, you can omit the direction in your MERGE.