簡體   English   中英

在 Neo4J 中使用 Cypher 連接兩個子查詢

[英]Joining two subqueries with Cypher in Neo4J

我有以下 SQL 查詢:

SELECT q1.customerId, q1.invoiceId, q2.workId, sum(q2.price)
FROM (select customer.id as customerId, invoice.id as invoiceId, work.id as workId from customer, invoice, workinvoice, work where customer.id=invoice.customerid and invoice.id=workinvoice.invoiceId and workinvoice.workId=work.id
) as q1, (select work.id as workId, sum((price * hours * workhours.discount) + (purchaseprice * amount * useditem.discount)) as price from worktype,workhours,work,warehouseitem,useditem where worktype.id=workhours.worktypeid and workhours.workid=work.id and work.id=useditem.workid and useditem.warehouseitemid=warehouseitem.id group by work.id
) as q2
WHERE q1.workId = q2.workId group by q1.invoiceId;

此查詢應返回每個客戶每張發票的工作價格總和。

我很想知道如何在 Neo4J 中進行這種查詢。 我知道有 UNION https://neo4j.com/docs/cypher-manual/current/clauses/union/ 但是,這似乎確實符合我的要求。 我需要創建兩個子查詢並從與該 SQL 示例中相同的節點加入它們。 使用 Cypher 執行此操作的正確方法是什么?

有一個非常復雜的示例,說明如何在 cypher 中進行連接,您可以在這里找到: https : //github.com/moxious/halin/blob/master/src/api/data/queries/dbms/3.5/tasks.js #L22

基本上,該技術是您運行第一個查詢,收集結果。 然后你運行第二個,收集結果。 然后展開第二個,使用過濾器匹配,並返回結果。

在真正簡化的形式中,它看起來像這樣:

CALL something() YIELD a, b
WITH collect({ a: a, b: b }) as resultSet1
CALL somethingElse YIELD a, c
WITH resultSet1, collect({ a: a, c: c }) as resultSet2

UNWIND resultSet2 as rec
WITH [item in resultSet1 WHERE item.a = rec.a][0] as match, rec

RETURN match.a, match.b, rec.c

列表理解位基本上是在做連接。 在這里,我們加入了“a”領域。

我想出了我想要的解決方案:

MATCH (inv:invoice)-[:WORK_INVOICE]->(w:work)<-[h:WORKHOURS]-(wt:worktype) WITH inv, w, SUM(wt.price * h.hours * h.discount)作為 workTimePrice OPTIONAL MATCH (w)-[u:USED_ITEM]->(i:item) WITH inv, workTimePrice + SUM(u.amount * u.discount * i.purchaseprice) as workItemPrice RETURN inv, sum(workItemPrice) as invoicePrice

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM