2017-10-11 48 views
0

我有成功運行一個查詢所有節點:Neo4j的查詢 - 查找其滿足資產狀況和有關係

match (n:A {tag_no:"N2203"})<-[:rel_a]-(v:B)-[:rel_b]->(r)<-[:rel_c]-(n) WHERE (v.invoice_date>="2012-08-01" AND v.invoice_date<"2016-02-01") with v, collect(r) as rs where all (x in rs where x.date<"2016-08-01") return count(v) as count;

我需要進一步過濾此查詢。

我需要相關r nodes with max(r.date) for each v,並找出有多少節點有型D

我想這個查詢的其他節點的關係,它拋出一個語法錯誤

match (n:A {tag_no:"N2203"})<-[:rel_a]-(v:B)-[:rel_b]->(r)<-[:rel_c]-(n) WHERE (v.invoice_date>="2012-08-01" AND v.invoice_date<"2016-02-01") with v, max(r.date) as date collect(r) as rs where (all (x in rs where x.date<"2016-08-01") AND filter(x in rs where x.date=date)[0]<-[:rel_c]-(d:D)) return count(v) as count;

我也試過很多其他的組合,但都拋出一些語法錯誤。所有的幫助表示讚賞。

+1

你的查詢將受益於格式化,這將使debuggind容易。請使用4個縮進空格(而不是三重反引號),並在查詢中使用更多的空格。我在https://gist.github.com/szarnyasg/8b53983b5ade8a31e4410f8f48ee3653創建並示例 –

回答

1

主要問題是,如果filter(x in rs where x.date=date)[0]確實是一個節點,查詢解析器現在無法使用,所以這在語法中是不允許的。幸運的是,有可能解決此在一些冗長的成本:

  • 使用另一張WITH條款引入一個別名(n)到節點變量。這將允許您使用WHERE <pattern>語法。
  • 此外,您不能在WHERE子句中引入新變量,因此只需使用(:D)而不是(d:D)

所以查詢看起來像這樣(很明顯,我沒有測試過):

MATCH (n:A {tag_no:"N2203"})<-[:rel_a]-(v:B)-[:rel_b]->(r)<-[:rel_c]-(n) 
WHERE v.invoice_date>="2012-08-01" 
    AND v.invoice_date<"2016-02-01" 
WITH 
    v, 
    max(r.date) AS date, 
    collect(r) AS rs 
WHERE all(x IN rs WHERE x.date<"2016-08-01") 
WITH filter(x in rs where x.date=date)[0] AS n, v 
WHERE (n)<-[:rel_c]-(:D) 
RETURN count(v) AS count;