Neo4j 密码查询、节点计数和双向关系
Neo4j cypher queries, counting nodes and two way relationships
我正在尝试 运行 cypher 对具有双向关系的节点进行查询,并且我还想对这些关系中的每一个进行计数。
示例:
Nodes: store + customer
Relationships: sold + bought
在理想情况下,我希望看到连接商店和客户的两种关系。
但是,在某些情况下,只有客户报告他们从商店购买,反之亦然,只有商店报告他们向客户出售,但客户尚未验证该销售。
我想return以下内容:
- 店铺名称
- 买卖关系的计数(商店 <—> 顾客)
- 仅售出关系的计数(商店 —> 客户)
- 仅购买关系的计数(商店 <— 客户)
*** 说明:
例如,这将是 10 家商店,每家商店有 7-10 位顾客,是的,在 Neo 中,它将是两条不同的弧连接:
store-customer r/ships
我们的目标是查看每家商店的不同报告做法,因为一些商店可能会说他们在没有卖的时候卖了东西,而顾客可能会说他们买了东西。每种情况发生的频率如何?
你可以使用像
这样的东西
match (n:Store)<-[r:bought]-(n1:Customer)<-[r1:sold]-(n) with count(distinct r) as verified
match (n:Store)<-[r:bought]-(n1:Customer) with verified, count(distinct r) - verified as boughtOnly
match (n:Store)-[r:sold]->(n1:Customer) with verified, boughtOnly, count(distinct r) - verified as soldOnly
return verified, soldOnly, boughtOnly
查看虚拟数据库 here 进行测试
编辑
使用 where 子句更新查询并返回 store
信息
match (n:Store) where n.state = 'MA' and n.city = 'Boston' with n as Store
match (Store)<-[r:bought]-(n1:Customer)<-[r1:sold]-(n) with Store, count(distinct r) as verified
match (Store)<-[r:bought]-(n1:Customer) with Store, verified, count(distinct r) - verified as boughtOnly
match (Store)-[r:sold]->(n1:Customer) with Store, verified, boughtOnly, count(distinct r) - verified as soldOnly
return verified, soldOnly, boughtOnly, Store
就这么简单:
MATCH (n:Customer)
RETURN
size((n)-[:BOUGHT|:SOLD]-()) AS bothRels,
size((n)-[:BOUGHT]-()) AS boughtRels,
size((n)-[:SOLD]-()) AS soldRels
╒══════════╤════════════╤══════════╕
│"bothRels"│"boughtRels"│"soldRels"│
╞══════════╪════════════╪══════════╡
│2 │1 │1 │
└──────────┴────────────┴──────────┘
我正在尝试 运行 cypher 对具有双向关系的节点进行查询,并且我还想对这些关系中的每一个进行计数。
示例:
Nodes: store + customer
Relationships: sold + bought
在理想情况下,我希望看到连接商店和客户的两种关系。 但是,在某些情况下,只有客户报告他们从商店购买,反之亦然,只有商店报告他们向客户出售,但客户尚未验证该销售。
我想return以下内容:
- 店铺名称
- 买卖关系的计数(商店 <—> 顾客)
- 仅售出关系的计数(商店 —> 客户)
- 仅购买关系的计数(商店 <— 客户)
*** 说明:
例如,这将是 10 家商店,每家商店有 7-10 位顾客,是的,在 Neo 中,它将是两条不同的弧连接:
store-customer r/ships
我们的目标是查看每家商店的不同报告做法,因为一些商店可能会说他们在没有卖的时候卖了东西,而顾客可能会说他们买了东西。每种情况发生的频率如何?
你可以使用像
这样的东西match (n:Store)<-[r:bought]-(n1:Customer)<-[r1:sold]-(n) with count(distinct r) as verified
match (n:Store)<-[r:bought]-(n1:Customer) with verified, count(distinct r) - verified as boughtOnly
match (n:Store)-[r:sold]->(n1:Customer) with verified, boughtOnly, count(distinct r) - verified as soldOnly
return verified, soldOnly, boughtOnly
查看虚拟数据库 here 进行测试
编辑
使用 where 子句更新查询并返回 store
信息
match (n:Store) where n.state = 'MA' and n.city = 'Boston' with n as Store
match (Store)<-[r:bought]-(n1:Customer)<-[r1:sold]-(n) with Store, count(distinct r) as verified
match (Store)<-[r:bought]-(n1:Customer) with Store, verified, count(distinct r) - verified as boughtOnly
match (Store)-[r:sold]->(n1:Customer) with Store, verified, boughtOnly, count(distinct r) - verified as soldOnly
return verified, soldOnly, boughtOnly, Store
就这么简单:
MATCH (n:Customer)
RETURN
size((n)-[:BOUGHT|:SOLD]-()) AS bothRels,
size((n)-[:BOUGHT]-()) AS boughtRels,
size((n)-[:SOLD]-()) AS soldRels
╒══════════╤════════════╤══════════╕
│"bothRels"│"boughtRels"│"soldRels"│
╞══════════╪════════════╪══════════╡
│2 │1 │1 │
└──────────┴────────────┴──────────┘