请注意,计数是累积的。因此,对于产品 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 │
└────────────┴──────────────┴──────────────────┴───────────────────┴────────────────────────┘