Cypher:Union 中的相关变量

Cypher: Correlated variables in Union

我需要回答以下查询:根据门店销售额和所有其他城市门店销售额的总和,排名前三的门店城市。

使用以下查询

// Top 3 cities
MATCH (t:Store)<-[:HasStore]-(s:Sales)
WITH t.StoreCity AS StoreCity, t, sum(s.StoreSales) AS StoreSales
ORDER BY sum(s.StoreSales) DESC LIMIT 3
// All other cities
WITH StoreCity, StoreSales, collect(t) AS TopThreeCities
MATCH (t1:Store)<-[:HasStore]-(s:Sales)
WHERE NOT(t1 IN TopThreeCities)
// JOIN of the two results -> I would need the UNION
RETURN StoreCity, StoreSales, "Other cities" AS StoreCity1, sum(s.StoreSales) AS StoreSales1

我得到了以下答案

"A" 10  "Other Cities"  50
"B" 9   "Other Cities"  50
"C" 8   "Other Cities"  50

但我想获得

"A" 10
"B" 9
"C" 8
"Other Cities"  50

知道如何获得这个吗?我尝试了很多可能性都没有成功:-(

使用联合:

// Top 3 cities
MATCH (t:Store)<-[:HasStore]-(s:Sales)
RETURN t.StoreCity AS StoreCity, sum(s.StoreSales) AS StoreSales
ORDER BY sum(s.StoreSales) DESC LIMIT 3
// All other cities
UNION
MATCH (t:Store)<-[:HasStore]-(s:Sales)
WITH t.StoreCity, sum(s.StoreSales) AS AllStoreSales
ORDER BY AllStoreSales DESC SKIP 3
RETURN "Other cities" AS StoreCity, sum(AllStoreSales) AS StoreSales

UNION 运行 有两个不同的查询,因此在 UNION 之前,我们会找到销量最高的三个城市和 return 它们的名称和数量,就像您最初

在 UNION 之后,相同的基本查询再次是 运行 以按城市查找销售额总和,按销售额降序排列,然后跳过前三个结果。将剩余城市的销售额相加并return计算为其他城市

UNION 的两个部分是完全独立的,但是两个查询都必须 return 具有相同列名和相同列顺序的相同数量的列。