如何使用Cypher neo4j将三列连接成一列并获取其中唯一条目的数量?

时间:2017-04-21 15:34:49

标签: neo4j cypher

我可以在Panama数据库中使用Cypher在Neo4j中查询三种类型的身份持有者(我定义该术语)的国家,即实体(公司),官员(股东)和中介(中间公司)三个属性/列。每列都有由冒号分隔的单个或双个条目(例如:英属维尔京群岛;俄罗斯)。我们希望将这些列中的国家/地区连接成一组独特的国家/地区,从而获得作为新属性的国家/地区数量。

为此,我从我对Cypher的理解中尝试了以下代码:

MATCH (BEZ2:Officer)-[:SHAREHOLDER_OF]->(BEZ1:Entity),(BEZ3:Intermediary)-[:INTERMEDIARY_OF]->(BEZ1:Entity)
WHERE BEZ1.address CONTAINS "Belize" AND 
  NOT ((BEZ1.countries="Belize" AND  BEZ2.countries="Belize" AND BEZ3.countries="Belize") OR
    (BEZ1.status IN ["Inactivated", "Dissolved shelf company", "Dissolved", "Discontinued", "Struck / Defunct / Deregistered", "Dead"]))
SET BEZ4.countries= (BEZ1.countries+","+BEZ2.countries+","+BEZ3.countries) 
RETURN BEZ3.countries AS IntermediaryCountries, BEZ3.name AS 
  Intermediaryname, BEZ2.countries AS OfficerCountries , BEZ2.name AS 
  Officername, BEZ1.countries as EntityCountries, BEZ1.name AS Companyname, 
  BEZ1.address AS CompanyAddress,DISTINCT count(BEZ4.countries) AS NoofConnections  

相关部分是第7行中的SET语句和最后一行中的DISTINCT计数。代码显示错误,这对我没有意义:输入'u'无效:预期'n / N'。我想这意味着可能会使用COLLECT,但我们也尝试过,它会在'u'和'n'之间显示错误。请帮助我们获得我们想要的输出,它使我们的工作很容易。提前谢谢!

编辑:考虑到我没有像@Cyber​​sam所建议的那样定义变量,我尝试了如下命令CREATE,但是它显示错误“无效输入'R':”用于命令RETURN。这对我来说是不可思议的。帮助真的需要,谢谢。

代码2:

MATCH (BEZ2:Officer)-[:SHAREHOLDER_OF]->(BEZ1:Entity),(BEZ3:Intermediary)-
[:INTERMEDIARY_OF]->(BEZ1:Entity)
WHERE BEZ1.address CONTAINS "Belize" AND 
NOT ((BEZ1.countries="Belize" AND  BEZ2.countries="Belize" AND 
BEZ3.countries="Belize") OR
(BEZ1.status IN ["Inactivated", "Dissolved shelf company", "Dissolved", 
"Discontinued", "Struck / Defunct / Deregistered", "Dead"]))
CREATE (p:Connections{countries: 
split((BEZ1.countries+";"+BEZ2.countries+";"+BEZ3.countries),";")
RETURN BEZ3.countries AS IntermediaryCountries, BEZ3.name AS 
Intermediaryname, BEZ2.countries AS OfficerCountries , BEZ2.name AS 
Officername, BEZ1.countries as EntityCountries, BEZ1.name AS Companyname, 
BEZ1.address AS CompanyAddress,  AS TOTAL, collect (DISTINCT 
COUNT(p.countries)) AS NumberofConnections

第8和第9行是新的并且正在进行检查。

1 个答案:

答案 0 :(得分:1)

第一次查询

您从未定义标识符DatabaseTransactions,因此您无法在其上设置属性。

第二个查询(应该已经在一个单独的问题中发布):

您有几个拼写错误和语法错误。

此查询不应出错(但您必须确定它是否符合您的要求):

BEZ4

原件问题:

  • MATCH (BEZ2:Officer)-[:SHAREHOLDER_OF]->(BEZ1:Entity),(BEZ3:Intermediary)- [:INTERMEDIARY_OF]->(BEZ1:Entity) WHERE BEZ1.address CONTAINS "Belize" AND NOT ((BEZ1.countries="Belize" AND BEZ2.countries="Belize" AND BEZ3.countries="Belize") OR (BEZ1.status IN ["Inactivated", "Dissolved shelf company", "Dissolved", "Discontinued", "Struck / Defunct / Deregistered", "Dead"])) CREATE (p:Connections {countries: split((BEZ1.countries+";"+BEZ2.countries+";"+BEZ3.countries), ";")}) RETURN BEZ3.countries AS IntermediaryCountries, BEZ3.name AS Intermediaryname, BEZ2.countries AS OfficerCountries , BEZ2.name AS Officername, BEZ1.countries as EntityCountries, BEZ1.name AS Companyname, BEZ1.address AS CompanyAddress, SIZE(p.countries) AS NumberofConnections; 条款错过了结束CREATE和结束}
  • )条款有一个悬空RETURN字词。
  • AS TOTAL试图执行嵌套聚合,但不支持。在任何情况下,即使它有效,它可能也不会返回你想要的。我怀疑你确实想要collect (DISTINCT COUNT(p.countries))集合的大小,所以这就是我在查询中使用的内容。