【问题标题】:How to add output of second query and third query with the output of First Query in neo4j如何在neo4j中将第二个查询和第三个查询的输出与第一个查询的输出相加
【发布时间】:2022-10-04 07:24:48
【问题描述】:

在表中,您有 product_id 和 order_created_date,这是一个 datetimestamp 字段。

现在我们需要如下输出。

对于每个 product_id,今天创建了多少订单,最近 5 天创建了多少订单,最近 30 天创建了多少订单,到今天创建的订单总数

Product_Id orders_today orders_last5days orders_last30days total_orders_tilltoday
101             5              20              100                 250
102             7              27              150                 450

【问题讨论】:

    标签: neo4j count sum


    【解决方案1】:

    请注意,计数是累积的。因此,对于产品 101,在过去 5 天内包括今天的 5 个订单,在过去 30 天内包括 25 个订单。

    我计算了 Product 中的所有 product_id,然后调用一个子查询来计算今天、5 天和 30 天以来的每一天。请注意,我使用了虚拟计数(例如:0 作为 orders_today),因为 UNION ALL 要求所有列的名称相同。

    最后,我对每个产品 ID 进行总计(总和)。请注意,总订单计数是重复的,因此无需获取总数。

    MATCH (n:Product)  
    WITH n.product_id as Product_Id, count(n) as total_orders_tilltoday 
    WITH Product_Id, total_orders_tilltoday ORDER BY Product_Id
    CALL {
        WITH Product_Id
        OPTIONAL MATCH (n:Product {product_id: Product_Id}) 
        WHERE  duration.inDays(date(n.order_created_date), date()).days <= 30
        WITH n.product_id as Product_Id, count(n) as orders_last30days
        RETURN   0 as orders_today, 0 as orders_last5days, orders_last30days 
        UNION ALL
        WITH Product_Id
        OPTIONAL MATCH (n:Product {product_id: Product_Id}) 
        WHERE  duration.inDays(date(n.order_created_date), date()).days <= 5
        WITH n.product_id as Product_Id, count(n) as orders_last5days
        RETURN   0 as orders_today, orders_last5days, 0 as orders_last30days 
        UNION ALL
        WITH Product_Id
        OPTIONAL MATCH (n:Product {product_id: Product_Id}) 
        WHERE  duration.inDays(date(n.order_created_date), date()).days <= 1
        WITH n.product_id as Product_Id, count(n) as orders_today
        RETURN  orders_today, 0 as orders_last5days, 0 as orders_last30days  
    }
    RETURN Product_Id,sum(orders_today) as orders_today,sum(orders_last5days) as orders_last5days,sum(orders_last30days) as orders_last30days,total_orders_tilltoday)
    

    使用 3 个产品 ID 的示例结果:

    ╒════════════╤══════════════╤══════════════════╤═══════════════════╤════════════════════════╕
    │"Product_Id"│"orders_today"│"orders_last5days"│"orders_last30days"│"total_orders_tilltoday"│
    ╞════════════╪══════════════╪══════════════════╪═══════════════════╪════════════════════════╡
    │101         │0             │0                 │2                  │2                       │
    ├────────────┼──────────────┼──────────────────┼───────────────────┼────────────────────────┤
    │102         │0             │1                 │1                  │1                       │
    ├────────────┼──────────────┼──────────────────┼───────────────────┼────────────────────────┤
    │103         │1             │1                 │1                  │1                       │
    └────────────┴──────────────┴──────────────────┴───────────────────┴────────────────────────┘
    

    【讨论】:

    • 如果我尝试上述方法,我在 CALL 处收到错误,即 Invalid input '{':expected whitespace, namespace of a procedure name(line 4, column 5(offset:163)) "CALL{"
    • 你的neo4j是什么版本的?子查询调用仅适用于版本 4 及更高版本。
    猜你喜欢
    • 2012-04-19
    • 2016-08-02
    • 1970-01-01
    • 2013-11-01
    • 2015-07-23
    • 1970-01-01
    • 2018-03-20
    • 1970-01-01
    • 2015-03-16
    相关资源
    最近更新 更多