【问题标题】:Neo4j cypher query for groupBy multiple entities对 groupBy 多个实体的 Neo4j 密码查询
【发布时间】:2016-10-01 09:55:39
【问题描述】:

Neo4j 密码查询以获取订单计数:groupBy 年份,然后是月份,然后是 org

    MATCH (year)-[:MONTH]->(month:Month) 
    WITH year,month 
    MATCH (month)-[r:DAY]->(day:Day) 
    WITH year,month,day 
    OPTIONAL MATCH (order:Order)-[r:ORD_CREATED]->(day) 
    WITH year,month,order
    MATCH (order)-[:ORDER_OF]-(org)
    RETURN  year,month,COLLECT(DISTINCT(org.name),count(order))

我怎样才能实现以下输出?任何帮助将不胜感激

 {
 year
 [{month1,[ {org1,20},{ org2,30}]}],
 [{month2,[ {org1,26},{ org2,10}]}],
}

【问题讨论】:

    标签: neo4j cypher


    【解决方案1】:

    也许不是最佳的,但对我有用:

    // match the path from an Org to the Day of order
    MATCH (day:Day)<-[:ORD_CREATED]-(order:Order)-[:ORDER_OF]-(org:Org)
    // count number of paths for each day & org 
    WITH day, org, count(*) as opd
    // match on the month for each day
    MATCH (day)<-[:DAY]-(month:Month)
    // sum the order counts for each month & org
    WITH month, org.name as name, SUM(opd) as opm
    // for each month, create a collection of org order counts
    WITH month, COLLECT({ org:name, cnt:opm }) as mon_cnt
    // match on the year for each month
    MATCH (month)<-[:MONTH]-(year:Year)
    // create the final structure of year, list of months, org order counts per month 
    RETURN year.year, COLLECT({ month:month.text, mon_cnt:mon_cnt });
    

    http://console.neo4j.org/r/y9qyzl 示例图和执行结果

    |year.year│COLLECT({ month:month.text, mon_cnt:mon_cnt })              │
    ╞═════════╪════════════════════════════════════════════════════════════╡
    │2015     │[{month: Feb 2015, mon_cnt: [{org: Org5, cnt: 2}, {org: Org2│
    │         │, cnt: 1}, {org: Org3, cnt: 1}, {org: Org4, cnt: 1}, {org: O│
    │         │rg1, cnt: 1}]}, {month: Jan 2015, mon_cnt: [{org: Org3, cnt:│
    │         │ 1}, {org: Org2, cnt: 1}, {org: Org4, cnt: 1}]}]            │
    ├─────────┼────────────────────────────────────────────────────────────┤
    │2016     │[{month: Feb 2016, mon_cnt: [{org: Org2, cnt: 1}, {org: Org1│
    │         │, cnt: 1}, {org: Org5, cnt: 1}]}]                           │
    

    【讨论】:

    • 感谢回复
    猜你喜欢
    • 2013-09-29
    • 2016-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多